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;
}

posted @ 2018-05-20 20:02  LJZ_C  阅读(328)  评论(0编辑  收藏  举报