QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#69701#2356. Partition of QueriesBowieSHITL 9ms9876kbC++142.1kb2022-12-30 15:26:162022-12-30 15:26:19

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:26:19]
  • 评测
  • 测评结果:TL
  • 用时:9ms
  • 内存:9876kb
  • [2022-12-30 15:26:16]
  • 提交

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-1])>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;
    }
    */
    ll tmp=0;
    for (int i=1;i<=tp;i++)
        tmp=max (tmp,k[stk[i]]*s2[x]+b[stk[i]]);
    return tmp;
    for (int i=max (1,l-5);i<=min (r+5,tp);i++)
        tmp=max (tmp,k[stk[i]]*s2[x]+b[stk[i]]);
    return tmp;
    /*
    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: 7716kb

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

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

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

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

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

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

input:

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

output:

0

result:

ok single line: '0'

Test #5:

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

input:

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

output:

19

result:

ok single line: '19'

Test #6:

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

input:

1 1
?

output:

0

result:

ok single line: '0'

Test #7:

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

input:

9 7
++++++++?

output:

7

result:

ok single line: '7'

Test #8:

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

input:

9 8
++++++++?

output:

8

result:

ok single line: '8'

Test #9:

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

input:

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

output:

15

result:

ok single line: '15'

Test #10:

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

input:

5 3
+?+?+

output:

3

result:

ok single line: '3'

Test #11:

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

input:

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

output:

10

result:

ok single line: '10'

Test #12:

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

input:

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

output:

12

result:

ok single line: '12'

Test #13:

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

input:

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

output:

14

result:

ok single line: '14'

Test #14:

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

input:

15 6
+?+?+??+??+??+?

output:

18

result:

ok single line: '18'

Test #15:

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

input:

19 8
+?+?+??+??+??+?++??

output:

28

result:

ok single line: '28'

Test #16:

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

input:

20 9
+?+?+??+??+??+?++???

output:

30

result:

ok single line: '30'

Test #17:

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

input:

500 100
+?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????++++...

output:

2710

result:

ok single line: '2710'

Test #18:

score: 0
Accepted
time: 9ms
memory: 9876kb

input:

10000 100
+?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????++...

output:

56616

result:

ok single line: '56616'

Test #19:

score: -100
Time Limit Exceeded

input:

500000 3000
+?+?+??+??+??+?++?????++??+?+????????????+++?+?+???++?+?+++++++????++??+??+++??++++?++??+??+??+?????++++???+??++?+?++?++???++++???????+??????????++?+??+?+++???+?+???++?++?++++???++?++?????+??????+?++???++??+?++?+??+??++??++??++?+?+?+++??+??++????+?++?++?++??+?+++?+?+???+?+++?++?++++?????...

output:


result: