CodeForces933A A Twisty Movement
给出一个长度为n的由1和2组成的序列问翻转一次任意[l,r]区间后最长的不下降子序列长度
观察发现最后答案序列为1...2...1...2则枚举分界点即可
#include <cstdio>
#include <iostream>
using namespace std;
int N;
int A[2050];
int sum1[2050], sum2[2050];
int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
}
for(int i = 1; i <= N; i++) {
if(A[i] == 1) sum1[i]++;
else sum2[i]++;
sum1[i] += sum1[i - 1];
sum2[i] += sum2[i - 1];
}
int ans = 0;
for(int i = 1; i <= N; i++) {
int maxn1 = 0, maxn2 = 0;
for(int j = 0; j <= i; j++)
maxn1 = max(maxn1, sum1[j] + sum2[i] - sum2[j]);
for(int j = i; j <= N; j++)
maxn2 = max(maxn2, sum1[j] - sum1[i] + sum2[N] - sum2[j]);
ans = max(ans, maxn1 + maxn2);
}
printf("%d\n", ans);
return 0;
}