Submission #1101548

#TimeUsernameProblemLanguageResultExecution timeMemory
1101548doducanhSplit the sequence (APIO14_sequence)C++14
33 / 100
78 ms131072 KiB
///breaker #pragma GCC optimize("O2") #include<bits/stdc++.h> using namespace std; #define int long long #define ll long long #define fi first #define se second #define ii pair<int,int> #define mp make_pair #define in(x) freopen(x,"r",stdin) #define out(x) freopen(x,"w",stdout) #define bit(x,i) ((x>>i)&1) #define lc (id<<1) #define rc ((id<<1)^1) const int maxn=1e6+7; ll dp[2][maxn]; int where[205][maxn]; ll a[maxn]; int n,k; bool case1(int x, int y, int i) { return (dp[0][y] - dp[0][x] >= (a[y] - a[x]) * (a[n] - a[i])); } bool case2(int x, int y, int i) { return ((dp[0][y] - dp[0][x]) * (a[i] - a[y]) <= (dp[0][i] - dp[0][y]) * (a[y] - a[x])); } void sol(void){ cin>>n>>k; a[0]=0; for(int i=1;i<=n;i++){ cin>>a[i]; a[i]+=a[i-1]; } for(int i=0;i<=n;i++){ dp[0][i]=dp[1][i]=0; } deque<int>dq; for(int i=1;i<=k;i++){ dq.push_back(i-1); for(int j=i;j<=n;j++){ while(dq.size()>1&&case1(dq[0],dq[1],j))dq.pop_front(); int tmp=dq.front(); where[i][j]=tmp; dp[1][j]=dp[0][tmp]+1ll*a[j]*(a[n]-a[j])-1ll*a[tmp]*(a[n]-a[j]); while(dq.size()>1&&case2(dq[dq.size()-2],dq.back(),j))dq.pop_back(); dq.push_back(j); } dq.clear(); for(int j=1;j<=n;j++)dp[0][j]=dp[1][j]; } ll mx=-1; int indx=-1; for(int i=1;i<=n;i++){ if(dp[0][i]>mx)mx=dp[0][i],indx=i; } vector<int>v; cout<<mx<<"\n"; for(int i=k;i>=1;i--){ v.push_back(indx); indx=where[i][indx]; } reverse(v.begin(),v.end()); for(int x:v)cout<<x<<" "; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t=1; while(t--){ sol(); } // you should actually read the stuff at the bottom return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#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...