Submission #551573

# Submission time Handle Problem Language Result Execution time Memory
551573 2022-04-21T04:54:03 Z hibiki Split the sequence (APIO14_sequence) C++11
100 / 100
738 ms 86596 KB
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second

struct LINE
{
    long long m,c;
    int idx;
};

int n,k,back[100010][205];
int qs[100010];
vector<LINE> stk;
queue<LINE> q[2];
int poi;

long long get_y(LINE p,long long x)
{
    return p.m*x+p.c;
}

long long chk(LINE a,LINE b,LINE c) // if positive xa > xb
{
    return (c.c-a.c)*(b.m-c.m) - (c.c-b.c)*(a.m-c.m);
}

void push(LINE p)
{
    while(stk.size()>=2 && chk(stk[stk.size()-2],stk[stk.size()-1],p)>0)
    {
        stk.pop_back();
        //printf("pop hap\n");
    }
    while(stk.size()>=1 && stk[stk.size()-1].m==p.m && stk[stk.size()-1].c<=p.c)
    {
        //printf("pop: %d %d\n",stk[cnt][stk[cnt].size()-1],p.c);
        stk.pop_back();
        //printf("pop hap\n");
    }
    stk.push_back(p);
}

pair<long long,int> query(long long x)
{
    if(poi>=stk.size())
        poi=stk.size()-1;
    //printf("poi%d: %d %d\n",cnt,poi[cnt],stk[cnt][poi[cnt]].idx);
    //printf("%d %d %d\n",poi[cnt],stk[cnt][poi[cnt]].idx,idx);
    while(poi+1<stk.size() && get_y(stk[poi],x)<=get_y(stk[poi+1],x))
        poi++;
    //printf("poi%d: %d %d\n",cnt,poi[cnt],stk[cnt][poi[cnt]].idx);
    return {get_y(stk[poi],x),stk[poi].idx};
}

long long qsl(int l,int r)
{
    return qs[r]-qs[l-1];
}

main()
{
    qs[0]=0;

    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&qs[i]);
        qs[i]=qs[i]+qs[i-1];
    }
    long long ans=0;
    int bb=-1;
    // for(int i=1;i<n;i++)
    // {
    //     //printf("-----%d-----\n",i);
    //     for(int j=min(i,k);j>=2;j--)
    //     {
    //     }
    // }
    LINE temp;
    for(int i=1;i<n;i++)
    {
        long long dp=qsl(1,i)*qsl(i+1,n);
        back[i][1]=0;
        //printf("1 %lld %d\n",dp[i][1],back[i][1]);
        temp.m=(long long)qs[i];
        temp.c=dp-(long long)qs[i]*(long long)qs[n];
        temp.idx=i;
        //printf("push: %lld %lld\n",temp.m,temp.c);
        q[1%2].push(temp);
        if(1==k&&ans<=dp)
        {
            ans=dp;
            bb=i;
        }
    }
    for(int j=2;j<=k;j++)
    {
        poi=0;
        for(int i=j;i<n;i++)
        {
            push(q[(j+1)%2].front());
            q[(j+1)%2].pop();
            pair<long long,int> ret=query(qs[i]);
            long long dp=ret.F+(long long)qs[i]*((long long)qs[n]-(long long)qs[i]);
            back[i][j]=ret.S;
            //printf("%d %lld %d\n",j,dp[i][j],back[i][j]);
            temp.m=qs[i];
            temp.c=dp-(long long)qs[i]*(long long)qs[n];
            temp.idx=i;
            //printf("push: %lld %lld\n",temp.m,temp.c);
            q[j%2].push(temp);
            if(j==k&&ans<=dp)
            {
                ans=dp;
                bb=i;
            }
        }
        q[(j+1)%2].pop();
        stk.clear();
    }
    int nw=k;
    printf("%lld\n",ans);
    while(bb)
    {
        printf("%d ",bb);
        bb=back[bb][k--];
    }
}

Compilation message

sequence.cpp: In function 'std::pair<long long int, int> query(long long int)':
sequence.cpp:47:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<LINE>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     if(poi>=stk.size())
      |        ~~~^~~~~~~~~~~~
sequence.cpp:51:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<LINE>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     while(poi+1<stk.size() && get_y(stk[poi],x)<=get_y(stk[poi+1],x))
      |           ~~~~~^~~~~~~~~~~
sequence.cpp: At global scope:
sequence.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   62 | main()
      | ^~~~
sequence.cpp: In function 'int main()':
sequence.cpp:123:9: warning: unused variable 'nw' [-Wunused-variable]
  123 |     int nw=k;
      |         ^~
sequence.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     scanf("%d %d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         scanf("%d",&qs[i]);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 212 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 212 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 212 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 212 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 212 KB contestant found the optimal answer: 1 == 1
7 Correct 0 ms 212 KB contestant found the optimal answer: 1 == 1
8 Correct 0 ms 212 KB contestant found the optimal answer: 1 == 1
9 Correct 0 ms 212 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 212 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 212 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 212 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 0 ms 212 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 212 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 0 ms 212 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 212 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 0 ms 212 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 0 ms 340 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 0 ms 340 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 0 ms 340 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 0 ms 340 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 0 ms 340 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 340 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 0 ms 340 KB contestant found the optimal answer: 687136 == 687136
9 Correct 0 ms 340 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 340 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 0 ms 468 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 468 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 1 ms 468 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 468 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 1 ms 468 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 1 ms 468 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 1 ms 468 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 468 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 468 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 468 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1108 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 1108 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 5 ms 1188 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 1108 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 7 ms 1192 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 6 ms 1108 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 8 ms 1108 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 5 ms 1108 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 2 ms 1108 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 3 ms 1108 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8916 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 6 ms 8916 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 66 ms 9112 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 6 ms 9112 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 45 ms 8952 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 43 ms 8948 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 47 ms 9112 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 36 ms 9112 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 44 ms 9104 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 55 ms 9116 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 48 ms 86572 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 44 ms 86492 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 636 ms 86476 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 46 ms 86468 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 738 ms 86556 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 516 ms 86508 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 552 ms 86532 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 414 ms 86596 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 496 ms 86568 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 628 ms 86512 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845