QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137424 | #2356. Partition of Queries | silly_1_2_3# | WA | 2ms | 7724kb | C++11 | 1.6kb | 2023-08-10 12:27:37 | 2023-08-10 12:28:11 |
Judging History
answer
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),i##z=(b);i<=i##z;i++)
#define ROF(i,a,b) for(int i=(a),i##z=(b);i>=i##z;i--)
#define REP(i,u) for(int i=hd[u],v;v=to[i],i;i=nxt[i])
#define temT template<typename T>
#define temT12 template<typename T1,typename T2>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
typedef long double ld;
const int N=int(1e6)+10;
const ll linf=ll(1e17)+10;
ll f0[N],pa[N],pq[N],f[N],g[N]; char a[N]; int n,y;
ll calc(ll j,ll i){
return f0[i]-f0[j]+y+f[j]+pa[j]*pq[j]-pa[j]*pq[i];
}
struct node{ int l,r,i; }s[N]; int hd,tl;
int check(int x,int i){
if(calc(i,s[x].r)>=calc(s[x].i,s[x].r))
return -1;
int l=s[x].l,r=s[x].r,mid,res;
while(l<=r){
mid=(l+r)/2;
if(calc(i,mid)<calc(s[x].i,mid))
res=mid,r=mid-1;
else
l=mid+1;
}
return res;
}
int main(){
cin>>n>>y; FOR(i,1,n) while(a[i]!='+' && a[i]!='?') cin>>a[i];
FOR(i,1,n) pa[i]=pa[i-1]+(a[i]=='+');
FOR(i,1,n) pq[i]=pq[i-1]+(a[i]=='?');
FOR(i,1,n) f0[i]=f0[i-1]+(a[i]=='?'?pa[i]:0);
/*
FOR(i,1,n){
f[i]=f0[i];
FOR(j,0,i-1) f[i]=min(f[i],f0[i]-f0[j]+y+f[j]+pa[j]*pq[j]-pa[j]*pq[i]);
}
*/
FOR(i,1,n) f[i]=f0[i]; f[0]=-y;
hd=tl=1,s[1]=(node){1,n,0};
FOR(i,1,n){
f[i]=min(f[i],calc(s[hd].i,i));
if(s[hd].l==i) s[hd].l++;
if(s[hd].l>s[hd].r) hd++;
int pos=s[hd].l;
ROF(x,tl,hd)
if(calc(i,s[x].l)>=calc(s[x].i,s[x].l)){ pos=check(x,i); break; }
if(pos!=-1){
while(s[tl].l>=pos && tl>=hd) tl--;
if(hd<=tl) s[tl].r=pos-1;
s[++tl]=(node){pos,n,i};
}
}
cout<<f[n];
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7440kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 7620kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2ms
memory: 7684kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 1ms
memory: 7544kb
input:
10 0 ++?+??++??
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 2ms
memory: 7616kb
input:
12 100 +?+++??+??++
output:
19
result:
ok single line: '19'
Test #6:
score: 0
Accepted
time: 0ms
memory: 7552kb
input:
1 1 ?
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 2ms
memory: 7556kb
input:
9 7 ++++++++?
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 2ms
memory: 7672kb
input:
9 8 ++++++++?
output:
8
result:
ok single line: '8'
Test #9:
score: 0
Accepted
time: 0ms
memory: 7508kb
input:
10 15 ++++++++??
output:
15
result:
ok single line: '15'
Test #10:
score: 0
Accepted
time: 2ms
memory: 7552kb
input:
5 3 +?+?+
output:
3
result:
ok single line: '3'
Test #11:
score: 0
Accepted
time: 2ms
memory: 7512kb
input:
10 5 +?+?+??+??
output:
10
result:
ok single line: '10'
Test #12:
score: -100
Wrong Answer
time: 0ms
memory: 7724kb
input:
10 7 +?+?+??+??
output:
14
result:
wrong answer 1st lines differ - expected: '12', found: '14'