Submission #158360

#TimeUsernameProblemLanguageResultExecution timeMemory
158360arnold518Split the sequence (APIO14_sequence)C++14
100 / 100
1753 ms87544 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e5; const int MAXK = 200; int N, K; ll A[MAXN+10], dp[MAXN+10], dp2[MAXN+10]; int memo[MAXN+10][MAXK+10]; struct CHT { void init() { S.clear(); pos=0; } struct Line { ll a, b, c; }; double cross(const Line &p, const Line &q) { return (double)(q.b-p.b)/(p.a-q.a); } vector<Line> S; void update(Line p) { if(!S.empty() && S[S.size()-1].a==p.a) { if(S[S.size()-1].b>=p.b) return; else S.pop_back(); } while(S.size()>1 && (S[S.size()-1].a!=p.a && cross(S[S.size()-1], p)<=cross(S[S.size()-2], S[S.size()-1]))) S.pop_back(); S.push_back(p); } int pos=0; pll query(ll x) { if(S.size()<=pos) pos=S.size()-1; else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++; return {S[pos].a*x+S[pos].b, S[pos].c}; } }; CHT cht; int main() { int i, j; scanf("%d%d", &N, &K); for(i=1; i<=N; i++) scanf("%lld", &A[i]), A[i]+=A[i-1]; for(j=1; j<=K; j++) { cht.init(); cht.update({A[j], dp2[j]-A[j]*A[j], j}); for(i=j+1; i<=N; i++) { pll t=cht.query(A[i]); dp[i]=t.first; memo[i][j]=t.second; cht.update({A[i], dp2[i]-A[i]*A[i], i}); } for(i=j+1; i<=N; i++) dp2[i]=dp[i]; //for(i=1; i<=N; i++) printf("%lld ", dp[i]); printf("\n"); } printf("%lld\n", dp[N]); int now=N; for(i=K; i>=1; i--) { now=memo[now][i]; printf("%d ", now); } }

Compilation message (stderr)

sequence.cpp: In member function 'pll CHT::query(ll)':
sequence.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(S.size()<=pos) pos=S.size()-1;
            ~~~~~~~~^~~~~
sequence.cpp:42:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
                    ~~~~~^~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:53:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) scanf("%lld", &A[i]), A[i]+=A[i-1];
                         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...