T1:
代码:
1 #include<cstdio> 2 #include<algorithm> 3 using std::max; 4 const int maxn=3e4+1e2,maxk=2e2+1e1; 5 const int inf = 0x3f3f3f3f; 6 7 enum Type {VALLY,UP,MOUNT,DOWN}; 8 int f[maxn][maxk][4]; // f[pos][sel][vally/up/mountain/down] 9 10 int main() { 11 static int n,k; 12 scanf("%d%d",&n,&k); 13 for(int i=1;i<=k;i++) for(int j=0;j<4;j++) f[0][i][j] = -inf; 14 for(int i=1,x;i<=n;i++) { 15 scanf("%d",&x); 16 for(int j=1;j<=k;j++) { 17 const int mul = 2 - ( j == 1 || j == k ); 18 f[i][j][VALLY] = max( f[i-1][j][VALLY] , f[i-1][j-1][DOWN] ) - mul * x; 19 f[i][j][UP] = max( f[i-1][j][UP] , f[i][j][VALLY] ); 20 f[i][j][MOUNT] = max( f[i-1][j][MOUNT] , f[i-1][j-1][UP] ) + mul * x; 21 f[i][j][DOWN] = max( f[i-1][j][DOWN] , f[i][j][MOUNT] ); 22 if( mul == 2 ) { // we can remove j . 23 f[i][j][UP] = max( f[i][j][UP] , f[i-1][j-1][UP] ); // end segment . 24 f[i][j][DOWN] = max( f[i][j][DOWN] , f[i-1][j-1][DOWN] ); 25 } 26 } 27 } 28 printf("%d ",max(f[n][k][UP],f[n][k][DOWN])); 29 return 0; 30 }
T2:
这题太神了,改不动,咕咕咕
T3:
这题太神了,改不动,咕咕咕