sicily 1176. Two Ends
2015-09-24 16:05
363 查看
1176. Two Ends
Constraints
Time Limit: 1 secs, Memory Limit: 64 MBDescription
In the two-player game "Two Ends", an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing thecard in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest -- we'll call this the greedy strategy. However, this is not always optimal, as the following
example shows: (The first player would win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.
Input
There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assumethat n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.
Output
For each test case you should print one line of output of the form:In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player's score and second player's score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger
end. If there is a tie, remove the left end.
Sample Input
4 3 2 10 4 8 1 2 3 4 5 6 7 8 8 2 2 1 5 3 8 7 3 0
Sample Output
In game 1, the greedy strategy might lose by as many as 7 points. In game 2, the greedy strategy might lose by as many as 4 points. In game 3, the greedy strategy might lose by as many as 5 points.
#include #include using namespace std; #define MAX 1000000 int arr[1001]; int maxdiff[1001][1001]; int dp(int begin, int end) { if (begin + 1 == end) { return abs(arr[begin] - arr[end]); } else if (maxdiff[begin][end] != MAX) { return maxdiff[begin][end]; } else { int t1, t2; // 假定第一个人选择左边(假定为左边是最优)。第二个人的选择分两种情况。然后递推if if (arr[begin + 1] >= arr[end]) { t1 = arr[begin] - arr[begin + 1] + dp(begin + 2, end); } // 假定第一个人选择右边(假定为右边是最优)。同上 else { t1 = arr[begin] - arr[end] + dp(begin + 1, end - 1); } if (arr[begin] >= arr[end - 1]) { t2 = arr[end] - arr[begin] + dp(begin + 1, end - 1); } else { t2 = arr[end] - arr[end - 1] + dp(begin, end - 2); } // 比较选左边和选右边哪个最优 maxdiff[begin][end] = t1 > t2 ? t1 : t2; return maxdiff[begin][end]; } } int main() { int n, count = 0;; cin >> n; while (n != 0) { count++; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) maxdiff[i][j] = MAX; } cout << "In game " << count << ", the greedy strategy might lose by as many as " << dp(0, n - 1) << " points." << endl; cin >> n; } return 0; }
相关文章推荐
- leetcode5
- a标签中有点击(onclick)事件
- 用sed或awk输出一段文字
- [LeetCode]题解(python):017-Letter Combinations of a Phone Number
- svn学习之一(svn独立服务器搭建)svn钩子了解
- Objective-C 学习笔记二
- chrome插件开发杂记
- 文件上传
- 为什么很多看起来不是很复杂的网站,比如 Facebook 需要大量顶尖高手来开发?
- linux 下 ethtool 修改网卡eeprom
- RecyclerView
- 创建一个sms.db数据库俩面在创建一个message表,插入数据然后在读取数据
- DOM解折XML文件
- 【Flume】flume于transactionCapacity和batchSize进行详细的分析和质疑的概念
- 转载:MyEclipse启动Tomcat缓慢的原因及解决办法
- [iOS]UIProgressView的高度
- 黑马程序员-继承、接口与多态
- Win7_x86_64位连接vmclient 提示vmrc控制台连接已断开
- date_format()的使用
- 深度学习入门简介