Submission #73912

#TimeUsernameProblemLanguageResultExecution timeMemory
73912zscoderHomecoming (BOI18_homecoming)C++17
62 / 100
1069 ms149700 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include "homecoming.h" using namespace std; using namespace __gnu_pbds; #define fi first #define se second #define mp make_pair #define pb push_back typedef long long ll; typedef pair<int,int> ii; typedef vector<int> vi; typedef long double ld; typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds; ll prefa[4222222]; ll prefb[4222222]; ll dp[2][2222222]; ll suma(int l, int r) { if(l==0) return prefa[r]; else return prefa[r]-prefa[l-1]; } ll sumb(int l, int r) { if(l>r) return 0; if(l==0) return prefb[r]; else return prefb[r]-prefb[l-1]; } const ll INF=ll(1e18); ll solve(int n, int k, int *a, int *b) { ll ans = 0; for(int i=0;i<2*n;i++) { prefa[i]=a[i%n]; prefb[i]=b[i%n]; if(i>0) { prefa[i]+=prefa[i-1]; prefb[i]+=prefb[i-1]; } } ans = max(0LL,prefa[n-1]-prefb[n-1]); if(k==n) return ans; for(int s=0;s<k;s++) { //s must be a starting point for(int j=0;j<2;j++) { for(int z=0;z<n;z++) { dp[j][z]=-INF; } } dp[1][s] = a[s] - sumb(s,s+k-1); ans=max(ans,dp[1][s]); if(s==k-1) { dp[0][s]=0; } for(int i=s+1;i<n;i++) { dp[0][i] = max(dp[0][i-1], dp[1][i-1]); dp[1][i] = max(dp[0][i-1] + a[i] - sumb(i, min(n+s-1, i+k-1)), dp[1][i-1] + a[i] - (i+k-1>=n+s?0:b[(i+k-1)%n])); //cerr<<0<<' '<<s<<' '<<i<<' '<<dp[0][s][i]<<'\n'; //cerr<<1<<' '<<s<<' '<<i<<' '<<dp[1][s][i]<<'\n'; ans=max(ans,dp[0][i]); ans=max(ans,dp[1][i]); } } return ans; } /* int aaaa[2111111]; int bbbb[2111111]; int main() { int n,k; cin>>n>>k; for(int i=0;i<n;i++) { cin>>aaaa[i]; } for(int i=0;i<n;i++) { cin>>bbbb[i]; } cout<<solve(n,k,aaaa,bbbb)<<'\n'; } */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...