QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#69694#2356. Partition of QueriesBowieSHIWA 0ms7664kbC++141.9kb2022-12-30 15:09:532022-12-30 15:09:55

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-30 15:09:55]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:7664kb
  • [2022-12-30 15:09:53]
  • 提交

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;
        cout<<l<<" "<<r<<endl;
        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 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 7664kb

input:

6 5
++??+?

output:

1 2
6

result:

wrong answer 1st lines differ - expected: '6', found: '1 2'