QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#69693 | #2356. Partition of Queries | BowieSHI | TL | 3ms | 9756kb | C++14 | 1.9kb | 2022-12-30 15:01:36 | 2022-12-30 15:01:37 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<vector>
#define INF 1e18
#define MAXN 1000010
using namespace std;
typedef long long ll;
int n;
ll y;
char s[MAXN];
ll s1[MAXN],s2[MAXN];
ll f[MAXN],ans;
ll k[MAXN],b[MAXN];
int stk[MAXN],tp;
double calc (int x,int u)
{
return 1.0*(b[u]-b[x])/(k[x]-k[u]);
}
void stk_push (int x)
{
while (tp>1)
{
if (calc (x,stk[tp])>calc (stk[tp],stk[tp-1])) tp--;
else break;
}
stk[++tp]=x;
}
ll get (int x)
{
if (tp==0) return -INF;
int l=1,r=tp;
while (l<r)
{
int mid=(l+r)>>1;
if ((mid==1||(s2[x]<=calc (stk[mid],stk[mid-1])&&mid>1))&&(mid==tp||(s2[x]>=calc (stk[mid],stk[mid+1])&&mid<tp)))
return k[stk[mid]]*s2[x]+b[stk[mid]];
if (mid-1>=1&&s2[x]>calc (stk[mid],stk[mid-1])) r=mid-1;
else if (mid+1<=tp&&s2[x]<calc (stk[mid],stk[mid+1])) l=mid+1;
}
if (l!=0&&l<=tp&&(l==1||(s2[x]<=calc (stk[l],stk[l-1])&&l>1))&&(l==tp||(s2[x]>=calc (stk[l],stk[l+1])&&l<tp)))
return k[stk[l]]*s2[x]+b[stk[l]];
return k[stk[r]]*s2[x]+b[stk[r]];
}
int main()
{
scanf ("%d%lld",&n,&y);
scanf ("%s",s+1);
for (int i=1;i<=n;i++)
{
s1[i]=s1[i-1];
if (s[i]=='+') s1[i]++;
}
for (int i=n;i>=1;i--)
{
s2[i]=s2[i+1];
if (s[i]=='?') s2[i]++;
}
for (int i=1;i<=n;i++)
if (s[i]=='?')
ans+=s1[i];
for (int i=1;i<=n;i++) f[i]=-INF;
for (int i=1;i<=n;i++)
{
if (s[i]=='?')
{
f[i]=max (s1[i]*s2[i]-y,-y+s1[i]*s2[i]+get (i));
k[i]=-s1[i],b[i]=f[i];
stk_push (i);
}
}
ll Max=0;
for (int i=1;i<=n;i++)
if (s[i]=='?')
Max=max (Max,f[i]);
printf ("%lld",ans-Max);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9756kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 3ms
memory: 7880kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3752kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Time Limit Exceeded
input:
10 0 ++?+??++??