标签:using cout inf algorithm for col hoc max def
思路:
博弈dp。
实现:
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 typedef long long ll; 6 7 const ll INF = 0x3f3f3f3f3f3f3f3f; 8 int a[100005], n; 9 ll dp[100005]; 10 11 ll dfs(int cur) 12 { 13 if (cur >= n) return 0; 14 if (dp[cur] != -1) return dp[cur]; 15 ll sum = 0, ans = -INF; 16 for (int i = 0; i < 3; i++) 17 { 18 sum += a[cur + i]; 19 ll minn = INF; 20 for (int j = 0; j < 3; j++) 21 { 22 minn = min(minn, dfs(cur + i + 1 + j + 1)); 23 } 24 ans = max(ans, sum + minn); 25 } 26 return dp[cur] = ans; 27 } 28 29 bool cmp(int a, int b) 30 { 31 return a > b; 32 } 33 34 int main() 35 { 36 while (cin >> n) 37 { 38 memset(dp, -1, sizeof dp); 39 for (int i = 0; i < n; i++) cin >> a[i]; 40 sort(a, a + n, cmp); 41 cout << dfs(0) << endl; 42 } 43 return 0; 44 }
标签:using cout inf algorithm for col hoc max def
原文地址:https://www.cnblogs.com/wangyiming/p/9852388.html