版本比较
第三次模考
正文
增加 66 行删除 0 行
326 行未改动全部展开327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396326 行未改动全部展开
- 对于 $10 \%$ 的数据, 满足 $n \leq 3$- 对于 $30 \%$ 的数据, 满足 $n \leq 20$- 对于 $80 \%$ 的数据, 满足 $n \leq 80$
```cpp// FZK 的代码#include <bits/stdc++.h>#define int long longusing namespace std;constexpr int N = 505;int n, a[N], dp[N][N][2], siz[N];bool vis[N][N][2];
void dfs(const int &p){ const int ls = (p << 1); const int rs = ((p << 1) | 1); for(int j = 0; j <= (n >> 1); j++) for(int c = 0; c < 2; c++) dp[p][j][c] = -1e18; if(ls <= n) dfs(ls); if(rs <= n) dfs(rs); if(ls > n and rs > n){ dp[p][0][0] = 0, vis[p][0][0] = true; dp[p][1][1] = 0, vis[p][1][1] = true; siz[p] = 1; }else if(ls == n){ siz[p] = siz[ls] + 1; for(int u = 0; u < 2; u ++) for(int l = 0; l < 2; l ++) for(int s = 0; s <= siz[ls]; s ++) if(vis[ls][s][l]){ const int tot = s + u; if(tot <= (n >> 1) and tot <= siz[p]){ const int val = dp[ls][s][l] + (u == l? a[ls]: 0); dp[p][tot][u] = max(dp[p][tot][u], val); vis[p][tot][u] = true; } } }else{ siz[p] = 1 + siz[ls] + siz[rs]; for(int u = 0; u < 2; u ++) for(int l = 0; l < 2; l ++) for(int r = 0; r < 2; r ++) for(int s = 0; s <= min(siz[ls], (n >> 1)); s ++) if(vis[ls][s][l]) for(int t = 0; t <= min(siz[rs], (n >> 1) - s); t ++) if(vis[rs][t][r]){ const int tot = s + t + u; if(tot <= (n >> 1) and tot <= siz[p]){ const int val = dp[ls][s][l] + (u == l? a[ls]: 0) + dp[rs][t][r] + (u == r? a[rs]: 0); dp[p][tot][u] = max(dp[p][tot][u], val); vis[p][tot][u] = true; } } }}
signed main(){ ios::sync_with_stdio(false); cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i]; dfs(1); cout << max(dp[1][n >> 1][0], dp[1][n >> 1][1]) << endl; return 0;} ```