KK's Number

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121332#problem/K

Description

Our lovely KK has a funny mathematical game:This game requires two people,There are numbers,every time KK will take the numbers,first.Every time you can take any number of the numbers.Until the number is taken.The minimum number of numbers is the score for each time.KK and the opponent's strategy is as much as possible to make their score minus the opponent's score more.In this case,How much is the final KK score minus the opponent's score?

Input

The first line of the input file contains an integer , which indicates the number of test cases.

For each test case, there are two lines,in the first line is a integer ,the other line has positive integers(no more than ).

Output

For each test case, there are one lines,includes a integer,indicating the final KK's score minus the opponent's score.

Sample Input

1

3

1 3 1

Sample Output

2

Hint

Firstly KK take 3;and the opponent take 1,1,so the result is 2.

题意:

给出N个数字,A和B两个人先后从中任取任意个数;

每次操作的权值为取出数中的最小数;

两人轮流取,直到N个数字取完;

两人均为最策略,要最大化权值差(自身-对方);

题解:

博弈DP:

先将数字升序排列:

dp[i]表示对于前i个数字能够得到的最大权值差(先手值-后手值);

转移方程为:

dp[i] = max(dp[i-1], num[i]-dp[i-1]);

解释:

对于当前的i,可以取1~i中的任意一个作为当前操作的权值;(因为取了某个数后,肯定会一并把比它大的数一起取掉);

dp[j-1]为取了当前num[j]后,对手在上一次所取到的最优值.

for(int j=1; j<=i; j++) {

dp[i] = max(dp[i], num[j] - dp[j-1]);

}

如果是上述n*n的dp过程,肯定会超时;

考虑一下,其实dp[i-1]覆盖了除了num[i]-dp[i-1]的其他所有情况.

所以只需要一维dp即可.

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <queue>
  7. #include <map>
  8. #include <set>
  9. #include <vector>
  10. #define LL long long
  11. #define eps 1e-8
  12. #define maxn 55000
  13. #define inf 0x3f3f3f3f
  14. #define IN freopen("in.txt","r",stdin);
  15. using namespace std;
  16. int n;
  17. int num[maxn];
  18. int dp[maxn];
  19. int main(int argc, char const *argv[])
  20. {
  21. //IN;
  22. int t; cin >> t;
  23. while(t--)
  24. {
  25. cin >> n;
  26. for(int i=1; i<=n; i++) scanf("%d", &num[i]);
  27. sort(num+1, num+1+n);
  28. memset(dp, 0, sizeof(dp));
  29. for(int i=1; i<=n; i++) {
  30. // for(int j=1; j<=i; j++) {
  31. // dp[i] = max(dp[i], num[j] - dp[j-1]);
  32. // }
  33. dp[i] = max(dp[i-1], num[i] - dp[i-1]);
  34. }
  35. printf("%d\n", dp[n]);
  36. }
  37. return 0;
  38. }

HDU 5623 KK's Number (博弈DP)的更多相关文章

  1. hdu 5623 KK's Number(dp)

    问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗10​4​​)个数,每次KK都会先拿数.每 ...

  2. HDU - 4597 Play Game(博弈dp)

    Play Game Alice and Bob are playing a game. There are two piles of cards. There are N cards in each ...

  3. HDU 3469 Catching the Thief (博弈 + DP递推)

    Catching the Thief Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU Problem D [ Humble number ]——基础DP丑数序列

    Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  5. 博弈dp入门 POJ - 1678 HDU - 4597

    本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...

  6. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  8. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  9. HDU 1231 最大连续子序列 --- 入门DP

    HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...

随机推荐

  1. Android开发之ProgressDialog与ProgressBar

    ProgressDialog,继承AlertDialog.所以ProgressDialog就是一个在对话框中显示ProgressDialog,并显示进度的文本信息. 并且没有取消和确定按钮,只能通过b ...

  2. [HDOJ4612]Warm up(双连通分量,缩点,树直径)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决 ...

  3. 面试题_66_to_75_Java IO 和 NIO 的面试题

    IO 是 Java 面试中一个非常重要的点.你应该很好掌握 Java IO,NIO,NIO2 以及与操作系统,磁盘 IO 相关的基础知识.下面是 Java IO 中经常问的问题. 66)在我 Java ...

  4. yeoman错误提示

    运行 yo angular 出现如下提示: $ yo angular grunt-cli: The grunt command line interface. (v0.1.9) Fatal error ...

  5. eclipse教程

    http://www.eclipse.org/downloads/eclipse-packages/http://wiki.eclipse.org/Eclipse_Articles,_Tutorial ...

  6. OLAP、OLTP的介绍和比较

    OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...

  7. TCSRM 593 div2(1000)(dp)

    Problem Statement      The pony Rainbow Dash wants to choose her pet. There are N animals who want t ...

  8. (转)博弈问题与SG函数

    博弈问题若你想仔细学习博弈论,我强烈推荐加利福尼亚大学的Thomas S. Ferguson教授精心撰写并免费提供的这份教材,它使我受益太多.(如果你的英文水平不足以阅读它,我只能说,恐怕你还没到需要 ...

  9. 结构体TABLE_share

    struct TABLE_share { static inline TABLE **next_ptr(TABLE *l) { return &l->share_next; } stat ...

  10. Jquery 弹出新窗体

    开始先用css将这个DIV设好位置,并且隐藏 function winshow() { var winNode = $(".win"); winNode.show("sl ...