Submission #950137

#TimeUsernameProblemLanguageResultExecution timeMemory
950137vjudge1Triple Jump (JOI19_jumps)C++17
19 / 100
448 ms150196 KiB
#include<bits/stdc++.h> using namespace std; const int N = (int)5e5+7; const int M = (int)2e5+7; const int inf = (int)1e9+7; int n, q, a[N]; int dp[5003][5003], mx[5003][5003]; int oo[M][20], lo[M]; pair<int, int> pref[N], suf[N]; int get(int l, int r) { if(l>r) { return -inf; } int x = lo[r-l+1]; return max(dp[l][x], dp[r-(1<<x)+1][x]); } void solve3() { cin >> q >> q >> q; for(int i = 2; i < M; ++i) { lo[i] = lo[i/2]+1; } for(int i = 1; i <= n; ++i) { oo[i][0] = a[i]; pref[i] = max(pref[i-1], {a[i], i}); } for(int i = n; i >= 1; --i) { suf[i] = max(suf[i+1], {a[i], i}); } for(int i = 2; i < 18; ++i) { for(int j = 1; j <= n-(1<<i)+1; ++j) { oo[i][j] = max(oo[i][j], oo[i+(1<<(j-1))][j-1]); } } int ans = 0; for(int i = 2; i < n; ++i) { int nxt = suf[i+1].second; ans = max(ans, a[i]+a[nxt]+get(max(1, i-(nxt-i)), i-1)); nxt = pref[i-1].second; if(i+(i-nxt)<=n) { ans = max(ans, a[i]+a[nxt]+get(i+(i-nxt), n)); } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 1; i <= n; ++i) { cin >> a[i]; } if(n>5000) { solve3(); return 0; } for(int i = 1; i <= n; ++i) { for(int j = i; j <= n; ++j) { mx[i][j] = max(mx[i][j-1], a[j]); } } for(int len = 3; len <= n; ++len) { for(int i = 1; i <= n-len+1; ++i) { int j = i+len-1; dp[i][j]=max(dp[i+1][j], dp[i][j-1]); int cur = a[i]+a[j]; int m = (i+j)/2; cur += mx[i+1][m]; dp[i][j] = max(dp[i][j], cur); } } cin >> q; while(q--) { int l, r; cin >> l >> r; cout << dp[l][r] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...