cf934C. A Twisty Movement(思维题)
程序员文章站
2022-04-08 19:16:06
题意 "题目链接" Sol 这题最直接的维护区间以0/1结尾的LIS的方法就不说了。 其实我们可以直接考虑翻转以某个位置为中点的区间的最大值 不难发现前缀和后缀产生的贡献都是独立的,可以直接算。维护一下前缀/后缀和即可 cpp include define Pair pair define MP(x ......
题意
sol
这题最直接的维护区间以0/1结尾的lis的方法就不说了。
其实我们可以直接考虑翻转以某个位置为中点的区间的最大值
不难发现前缀和后缀产生的贡献都是独立的,可以直接算。维护一下前缀/后缀和即可
#include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second #define ll long long #define ull unsigned long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int maxn = 501, mod = 1e9 + 7, inf = 1e9 + 10; const double eps = 1e-9; template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;} template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;} template <typename a> inline void debug(a a){cout << a << '\n';} template <typename a> inline ll sqr(a x){return 1ll * x * x;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, a[maxn], f[maxn], g[maxn]; signed main() { n = read(); for(int i = 1; i <= n; i++) a[i] = read(), f[i] = f[i - 1] + (a[i] == 1); for(int i = n; i >= 1; i--) g[i] = g[i + 1] + (a[i] == 2); int ans = 0; for(int i = 1; i <= n; i++) { int s1 = 0, s2 = 0; for(int j = i; j >= 1; j--) chmax(s1, f[j - 1] + g[j] - g[i]); for(int j = i; j <= n; j++) chmax(s2, g[j + 1] + f[j] - f[i - 1]); chmax(ans, max(s1 + s2, f[i - 1] + g[i])); } cout << ans; return 0; }