QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#22306 | #2356. Partition of Queries | WhybullYMe# | WA | 6ms | 12040kb | C++14 | 1.5kb | 2022-03-09 14:56:59 | 2022-04-30 00:52:44 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ri int
typedef long long ll;
const int maxn=1e6+10;
template<class T>inline bool ckmin(T &x,const T &y){return x>y?x=y,1:0;}
template<class T>inline bool ckmax(T &x,const T &y){return x<y?x=y,1:0;}
template<class T>inline void clear(T *arr,int siz,int val=0){memset(arr,val,sizeof(T)*(siz+1));}
template<typename T>
struct dq{
int hd,tl;
T v[maxn];
inline dq(){hd=1;}
inline T front(int ex=0){return v[hd+ex];}
inline T back(int ex=0){return v[tl-ex];}
inline void pop_front(){++hd;}
inline void pop_back(){--tl;}
inline void push(T k){v[++tl]=k;}
inline int size(){return tl-hd+1;}
inline void clear(){hd=1,tl=0;}
};
dq<int>q;
int a[maxn],m,n,tot;
char s[maxn];
ll ans,f[maxn],suf[maxn],suf2[maxn];
inline ll K(int k){return n-k+1;}
inline ll X(int k){return suf[k+1];}
inline ll Y(int k){return f[k]+suf2[k+1];}
inline double slope(int u,int v){return 1.*(Y(v)-Y(u))/(X(v)-X(u));}
int main(){
scanf("%d%d%s",&n,&m,s+1);
for(ri i=1,j=0;i<=n;++i){
if(s[i]=='?')a[++tot]=j,j=0;
else ++j;
}
n=tot;
for(ri i=n;i;--i){
suf[i]=suf[i+1]+a[i];
suf2[i]=suf2[i+1]+1ll*a[i]*(n-i+1);
}
ans=suf2[1];
q.push(0);
for(ri i=1;i<=n;++i){
while(q.size()>1&&slope(q.front(),q.front(1))>K(i))q.pop_front();
ri j=q.front();
f[i]=f[j]+suf2[j+1]-suf2[i]-(n-i+1)*(suf[j+1]-suf[i])+m;
ckmin(ans,f[i]+suf2[i+1]);
while(q.size()>1&&slope(q.back(1),q.back())>slope(q.back(),i))q.pop_back();
q.push(i);
}
printf("%lld",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 6ms
memory: 11924kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 5ms
memory: 11988kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 1ms
memory: 9988kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 5ms
memory: 11880kb
input:
10 0 ++?+??++??
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 1ms
memory: 11924kb
input:
12 100 +?+++??+??++
output:
19
result:
ok single line: '19'
Test #6:
score: 0
Accepted
time: 3ms
memory: 12036kb
input:
1 1 ?
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 3ms
memory: 12040kb
input:
9 7 ++++++++?
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 3ms
memory: 11884kb
input:
9 8 ++++++++?
output:
8
result:
ok single line: '8'
Test #9:
score: 0
Accepted
time: 2ms
memory: 11952kb
input:
10 15 ++++++++??
output:
15
result:
ok single line: '15'
Test #10:
score: 0
Accepted
time: 2ms
memory: 12036kb
input:
5 3 +?+?+
output:
3
result:
ok single line: '3'
Test #11:
score: 0
Accepted
time: 3ms
memory: 12040kb
input:
10 5 +?+?+??+??
output:
10
result:
ok single line: '10'
Test #12:
score: 0
Accepted
time: 3ms
memory: 11988kb
input:
10 7 +?+?+??+??
output:
12
result:
ok single line: '12'
Test #13:
score: -100
Wrong Answer
time: 3ms
memory: 11992kb
input:
15 4 +?+?+??+??+??+?
output:
16
result:
wrong answer 1st lines differ - expected: '14', found: '16'