QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137298 | #2356. Partition of Queries | PlentyOfPenalty# | WA | 2ms | 7940kb | C++20 | 1.0kb | 2023-08-10 09:55:31 | 2023-08-10 09:55:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
struct Line{
int k;
long long b;
long long F(int x){
return 1ll*k*x+b;
}
}q[N+10];
int n,y;
int m,num[N+10],nw,sz;
long long sum[N+10],dp[N+10];
char s[N+10];
bool Cov(Line l1,Line l2,Line l3){
return 1ll*(l2.b-l1.b)*(l1.k-l3.k)>=(l3.b-l1.b)*(l1.k-l2.k);
}
void Ins(Line l){
//cout<<"INS "<<l.k<<"x+"<<l.b<<"\n";
if(sz<nw)return(void)(q[++sz]=l);
if(q[sz].k==l.k){
if(q[sz].b>=l.b)q[sz]=l;
return;
}
while(sz>nw&&Cov(l,q[sz-1],q[sz]))--sz;
q[++sz]=l;
}
int main(){
scanf("%d%d",&n,&y);
scanf("%s",s+1);
for(int i=1;i<=n;++i){
if(s[i]=='?')++m,num[m]=nw,sum[m]=sum[m-1]+nw;
else ++nw;
}
Ins((Line){0,0});
nw=1;
for(int i=0;i<=m;++i){
while(nw<sz&&q[nw].F(i)>=q[nw+1].F(i))++nw;
//cout<<"Line="<<q[nw].k<<"x+"<<q[nw].b<<"\n";
dp[i]=q[nw].F(i)+sum[i]+y;
//cout<<"DP "<<i<<"="<<dp[i]<<"\n";
if(i<m)Ins((Line){-num[i+1],1ll*i*num[i+1]-sum[i]+dp[i]});
}
printf("%lld",dp[m]-y);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7688kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 0ms
memory: 7940kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2ms
memory: 7764kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 0ms
memory: 5632kb
input:
10 0 ++?+??++??
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 2ms
memory: 5724kb
input:
12 100 +?+++??+??++
output:
19
result:
ok single line: '19'
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 7804kb
input:
1 1 ?
output:
1
result:
wrong answer 1st lines differ - expected: '0', found: '1'