QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#69695#2356. Partition of QueriesBowieSHIWA 4ms9884kbC++141.8kb2022-12-30 15:10:282022-12-30 15:10:31

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:10:31]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:9884kb
  • [2022-12-30 15:10:28]
  • 提交

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 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: 3ms
memory: 7756kb

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 3ms
memory: 7840kb

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3476kb

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 3ms
memory: 7580kb

input:

10 0
++?+??++??

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 1ms
memory: 7836kb

input:

12 100
+?+++??+??++

output:

19

result:

ok single line: '19'

Test #6:

score: 0
Accepted
time: 1ms
memory: 9884kb

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 1ms
memory: 7572kb

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

score: 0
Accepted
time: 2ms
memory: 9700kb

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

score: 0
Accepted
time: 0ms
memory: 7568kb

input:

10 15
++++++++??

output:

15

result:

ok single line: '15'

Test #10:

score: 0
Accepted
time: 3ms
memory: 9848kb

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

score: 0
Accepted
time: 4ms
memory: 9616kb

input:

10 5
+?+?+??+??

output:

10

result:

ok single line: '10'

Test #12:

score: 0
Accepted
time: 3ms
memory: 7644kb

input:

10 7
+?+?+??+??

output:

12

result:

ok single line: '12'

Test #13:

score: -100
Wrong Answer
time: 3ms
memory: 7568kb

input:

15 4
+?+?+??+??+??+?

output:

15

result:

wrong answer 1st lines differ - expected: '14', found: '15'