Codeforces 934C(区间DP)

Codeforces 934C
题意:给你一个序列,你能任意翻转一个区间,要求使得翻转后的区间最长不下降子序列长度最长

正着求一次最长不下降子序列,最长不上升子序列,然后翻转区间等价于把这个区间的最长不下降子序列长度换成最长不上升子序列,枚举区间翻转即可

Codeforces Submission

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
//cf only
#define LD "%I64d"
#define D "%d"
#define pr printf
#define sc scanf
#define pty printf("YES\n")
#define ptn printf("NO\n")
using namespace std;
int read() {int x=0;char ch=getchar();while(ch<'0'||ch>'9'){ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x;}
LL gcd(LL a, LL b) {return b == 0 ? a : gcd(b, a % b);}
int ai[2000 + 5];
int n;
int c[2000 + 5][2000 + 5];
int d[2000 + 5][2000 + 5];
void cntup(int l) {
int max1 = 0, maxans = 0;
for (int r = l; r <= n; r++) {
if (ai[r] == 1) max1++;
if (ai[r] == 2) maxans = max(maxans + 1, max1 + 1);
c[l][r] = max(max1, maxans);
}
}
void cntdown(int l) {
int max2 = 0, maxans = 0;
for (int r = l; r <= n; r++) {
if (ai[r] == 2) max2++;
if (ai[r] == 1) maxans = max(maxans + 1, max2 + 1);
d[l][r] = max(max2, maxans);
}
}
void clean() {
}
void solve() {
clean();
for (int i = 1; i <= n; i++) scanf("%d", &ai[i]);
for (int i = 1; i <= n; i++) cntup(i), cntdown(i);
int ans = c[1][n];
for (int l = 1; l <= n; l++)
for (int r = l + 1; r <= n; r++) {
int tmp = c[1][n] - c[l][r] + d[l][r];
if (tmp > ans) ans = tmp;
}
printf("%d\n", ans);
}
int main() {
scanf("%d\n", &n), solve();
return 0;
}

------ 本文结束 ------