1176. Two Ends

题目链接地址:http://soj.me/1176

题目大意:两头取数。第一个人随机取,第二个人用贪婪算法(每次都取大的),求两人取数在第一个人赢的情况下的最大分差。使用贪婪算法时,如果左右两边相等,取左边的。

核心算法:动态规划。

设数组arr[a][b]是在数列区间[a,b]上的最大分差。

递推公式:

1.第一个人取左边的数arr[a]

if(arr[a+1] >=  arr[b])  point1 = input[a] - input[a+1] + dp(a+2,b);

else point1 = input[a] - input[b] + dp(a+1, b-1);

2.第一个人取右边的数arr[b]

if (input[a] >= input[b-1]) point2 = input[b] - input[a] + dp(a+1,b-1);

else point2 = input[b] - input[b-1] + dp(a,b-2);

arr[a][b] = point1 > point2 ? point1 : point2;

代码:

 1 // 动态规划!
 2 #include <stdio.h>
 3
 4 #define N 1005
 5 #define ENDLESS 100000
 6
 7 int arr[N][N];
 8 int input[N];
 9
10 int dp(int a, int b) {
11     if (arr[a][b] != ENDLESS)
12         return arr[a][b];
13
14     if (a > b)
15         return 0;
16
17     // 第一个人取左边的数,第二个人贪婪算法
18     int point1;
19     if (input[a+1] >= input[b]) {
20         point1 = input[a] - input[a+1] + dp(a+2,b);
21     } else {
22         point1 = input[a] - input[b] + dp(a+1, b-1);
23     }
24
25     // 第一个人取右边的数,第二个人贪婪算法
26     int point2;
27     if (input[a] >= input[b-1]) {
28         point2 = input[b] - input[a] + dp(a+1,b-1);
29     } else {
30         point2 = input[b] - input[b-1] + dp(a,b-2);
31     }
32
33     arr[a][b] = point1 > point2 ? point1 : point2;
34
35     return arr[a][b];
36 }
37
38 int main() {
39     int n;
40     int count = 0;
41     while (scanf("%d", &n) != EOF) {
42         if (n == 0)
43             break;
44
45         for (int i = 0; i < n; i++)
46             scanf("%d", &input[i]);
47
48         for (int i = 0; i < n; i++)
49             for (int j = 0; j < n; j++)
50                 arr[i][j] = ENDLESS;
51
52         printf("In game %d, the greedy strategy might lose by as many as %d points.\n", ++count, dp(0,n-1));
53     }
54     return 0;
55 }
时间: 2024-10-01 04:55:00

1176. Two Ends的相关文章

sicily 1176 two ends 动态规划解题

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 c

Sicily 1176 Two Ends

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 c

sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

DescriptionIn 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. T

SOJ 1176 Two Ends

题目大意:首先输入n(n ≤ 1000),n为偶数,接着输入n个整数,n个整数的和不超过1,000,000.两个人每次只能从两端取数,第一个人A可以用任意策略,第二个人B用贪心策略(左右数相等取左数).求在保证第一个人取得的和最大的前提下,两人取数和之差的最大值. 解题思路:动态规划.突破口在于A能取两头的数,B只能取两头最大数(相同取左),而每次取完一个数,能取数的区间就会减少一个.对A来说,每次取数有两种选择,如果A知道在更少区间范围的最优和之差,则可以作出最优的选择.对B来说,每次只能用贪

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

【BZOJ 1176】【Balkan 2007】Mokia

http://www.lydsy.com/JudgeOnline/problem.php?id=1176 整体二分的例题 把每个询问拆成四个询问,整体二分里x坐标递增,按x坐标扫的时候用树状数组维护y坐标前缀和. 一开始想复杂了,按cdq分治先solve左边再处理中间再solve右边,这样每次都要对x坐标排序,常数巨大,T了好几次TwT 后来参考了别人的代码,发现自己一开始就想复杂了.这道题不需要在solve完后还是保持原来的按x坐标递增的顺序,也不需要先处理出左边的信息才能更新右边的信息. 这

[2016-03-29][HDU][1176][免费馅饼]

时间:2016-03-29 09:46:34 星期二 题目编号:[2016-03-29][HDU][1176][免费馅饼] #include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int maxt = 100000 + 10; int dp[maxt][11]; int a[maxt][11]; int main(){ int n,x,t,maxT; whi

Project Euler 104:Pandigital Fibonacci ends 两端为全数字的斐波那契数

Pandigital Fibonacci ends The Fibonacci sequence is defined by the recurrence relation: F[n] = F[n-1] + F[n-2], where F[1] = 1 and F[2] = 1. It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digi