제출 #1134775

#제출 시각아이디문제언어결과실행 시간메모리
1134775kevinyangCandies (JOI18_candies)C++20
100 / 100
113 ms11432 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; signed main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int>a(n+1); for(int i = 1; i<=n; i++){ cin >> a[i]; } auto merge = [&](vector<int>&a, vector<int>&b) -> vector<int> { int n = a.size(); int m = b.size(); vector<int>c(n+m-1); int l = 0; int r = 0; for(int i = 1; i<n+m-1; i++){ if(l == n-1)r++; else if(r == m-1)l++; else if(a[l+1] + b[r] > a[l] + b[r+1])l++; else r++; c[i] = a[l] + b[r]; } return c; }; auto sigma = [&](vector<int>&a, vector<int>&b) -> vector<int> { int n = a.size(); int m = b.size(); vector<int>c(max(n,m)); for(int i = 0; i<max(n,m); i++){ if(i<n)c[i] = max(c[i],a[i]); if(i<m)c[i] = max(c[i],b[i]); } return c; }; auto solve = [&](auto self, int lx, int rx) -> vector<vector<vi>> { if(rx-lx <= 8){ vector<vector<vi>>res(2,vector<vi>(2)); for(int l = 0; l<2; l++){ for(int r = 0; r<2; r++){ int len = rx-lx-l-r; vector<vector<int>>dp(len+1,vector<int>(5,-(int)1e18)); dp[1][1] = a[lx+l]; dp[0][0] = 0; dp[1][0] = 0; for(int i = 2; i<=len; i++){ for(int j = 0; j<5; j++){ dp[i][j] = dp[i-1][j]; } for(int j = 0; j<4; j++){ dp[i][j+1] = max(dp[i][j+1],dp[i-2][j] + a[i+lx+l-1]); } } for(int j = 0; j<5; j++){ if(dp[len][j] > -(int)1e17){ res[l][r].push_back(dp[len][j]); } } } } return res; } int m = (lx+rx)/2; auto a = self(self,lx,m); auto b = self(self,m,rx); vector<vector<vi>>res(2,vector<vi>(2)); for(int l = 0; l<2; l++){ for(int r = 0; r<2; r++){ vi op1 = merge(a[l][0],b[1][r]); vi op2 = merge(a[l][1],b[0][r]); res[l][r] = sigma(op1,op2); } } return res; }; auto res = solve(solve,1,n+1); for(int i = 1; i<=(n+1)/2; i++){ cout << res[0][0][i] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...