标签:cst ring algorithm style space for pre return max
思路:
dp。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 const int MAXN = 100005; 7 const int INF = 0x3f3f3f3f; 8 int dp[5], n, T, p[MAXN]; 9 10 int solve() 11 { 12 for (int i = 0; i < 4; i++) 13 { 14 dp[i] = -INF; 15 } 16 dp[4] = 0; 17 for (int i = n - 1; i >= 0; i--) 18 { 19 dp[0] = max(dp[4], max(dp[3] - p[i], max(dp[2], max(dp[0], dp[1] - p[i])))); 20 dp[1] = max(dp[4] + p[i], max(dp[3], max(dp[1], dp[2] + p[i]))); 21 dp[2] = max(dp[4], max(dp[2], dp[3] - p[i])); 22 dp[3] = max(dp[3], dp[4] + p[i]); 23 dp[4] = dp[4]; 24 } 25 return dp[0]; 26 } 27 int main() 28 { 29 scanf("%d", &T); 30 while (T--) 31 { 32 scanf("%d", &n); 33 for (int i = 0; i < n; i++) 34 { 35 scanf("%d", &p[i]); 36 } 37 cout << solve() << endl; 38 } 39 return 0; 40 }
标签:cst ring algorithm style space for pre return max
原文地址:http://www.cnblogs.com/wangyiming/p/6381449.html