Sicily 1176. Two Ends
来源:互联网 发布:sql查询过滤重复记录 编辑:程序博客网 时间:2024/05/21 09:06
1176. Two Ends
Constraints
Time Limit: 1 secs, Memory Limit: 64 MB
Description
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 the card 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 assume that 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 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30
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.
// Problem#: 1176// Submission#: 3290209// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>using namespace std;const int MAX_N = 1005;int dp[MAX_N][MAX_N];int num[MAX_N];int goDp(int l, int r) { if (dp[l][r]) return dp[l][r]; if (l > r) return 0; int p1, p2; if (num[l + 1] >= num[r]) p1 = goDp(l + 2, r) + num[l] - num[l + 1]; else p1 = goDp(l + 1, r - 1) + num[l] - num[r]; if (num[l] >= num[r - 1]) p2 = goDp(l + 1, r - 1) + num[r] - num[l]; else p2 = goDp(l, r - 2) + num[r] - num[r - 1]; dp[l][r] = max(p1, p2); return dp[l][r];}int main() { std::ios::sync_with_stdio(false); int counter = 0; while (1) { counter++; int N; cin >> N; if (!N) break; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = 0; } } for (int i = 0; i < N; i++) cin >> num[i]; cout << "In game " << counter << ", the greedy strategy might lose by as many as " << goDp(0, N - 1) << " points." << endl; } //cin >> N; return 0;}
- Sicily 1176. Two Ends
- sicily 1176.Two ends
- [sicily]1176. Two Ends
- Sicily 1176. Two Ends
- Sicily 1176. Two Ends
- Sicily 1176. Two Ends
- sicily 1176. Two Ends
- sicily 1176. Two Ends
- sicily 1176. Two Ends
- Sicily 1176. Two Ends
- Sicily 1176. Two Ends ---by rupeng
- Sicily 1008 two ends
- Sicily 1176 Two Ends
- sicily 1176 two ends
- Sicily 1176 Two Ends
- sicily 1176 Two ends
- 1176. Two Ends
- 1176. Two Ends
- 关于Linux邮件列表的订阅与取消订阅具体方法
- 9个主流的开源许可协议
- 异常与自定义异常
- Sicily 1196. Conflict
- 11-语言入门-11-水仙花数
- Sicily 1176. Two Ends
- ListView拖拽交换 item 的实现(QQ 分组管理功能)
- Sicily 1142. 排序
- java wsdl反向生成源码,并使用CXF实现客户端调用代码
- MVC学习笔记_02
- 《八月未央》阅读笔记
- Sicily 1121. Tri Tiling
- Codeforces 527C Glass Carving(Set)
- linux apache web站点配置详解