把每一段单调序列分成一组可以接近最优质。
然后在此基础上讨论这一段的单调序列的两个端点该分到哪个序列里面,记录一下最优值。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <iomanip> #pragma comment(linker,"/STACK:1024000000"); #define EPS (1e-6) #define LL long long #define ULL unsigned long long #define _LL __int64 #define INF 0x3f3f3f3f #define Mod 1000000007 #define mod 1000000007 /** I/O Accelerator Interface .. **/ #define g (c=getchar()) #define d isdigit(g) #define p x=x*10+c-'0' #define n x=x*10+'0'-c #define pp l/=10,p #define nn l/=10,n template<class T> inline T& RD(T &x) { char c; while(!d); x=c-'0'; while(d)p; return x; } template<class T> inline T& RDD(T &x) { char c; while(g,c!='-'&&!isdigit(c)); if (c=='-') { x='0'-g; while(d)n; } else { x=c-'0'; while(d)p; } return x; } inline double& RF(double &x) //scanf("%lf", &x); { char c; while(g,c!='-'&&c!='.'&&!isdigit(c)); if(c=='-')if(g=='.') { x=0; double l=1; while(d)nn; x*=l; } else { x='0'-c; while(d)n; if(c=='.') { double l=1; while(d)nn; x*=l; } } else if(c=='.') { x=0; double l=1; while(d)pp; x*=l; } else { x=c-'0'; while(d)p; if(c=='.') { double l=1; while(d)pp; x*=l; } } return x; } #undef nn #undef pp #undef n #undef p #undef d #undef g using namespace std; int num[1000010]; int Judge(int x,int y) { if(x == y) return 0; if(x < y) return -1; return 1; } _LL dp[2][1000010]; int main() { int n,i,j; scanf("%d",&n); for(i = 1;i <= n; ++i) scanf("%d",&num[i]); int pre; memset(dp,0,sizeof(dp)); for(num[n+1] = num[n],pre = 1,i = 2;i <= n; ++i) { if(Judge(num[pre],num[i])*Judge(num[i],num[i+1]) == -1 || i == n) { dp[0][i] = max(dp[1][pre] + abs(num[i]-num[pre]),dp[0][pre] + abs(num[pre+1]-num[i])); dp[1][i] = max(dp[0][pre],dp[1][pre]); dp[1][i] = max(dp[1][i],dp[1][pre]+abs(num[pre]-num[i-1])); if(pre+1 <= i-1) dp[1][i] = max(dp[1][i],dp[0][pre] + abs(num[pre+1]-num[i-1])); pre = i; } } printf("%I64d\n",max(dp[0][n],dp[1][n])); return 0; }
时间: 2024-11-05 14:59:56