HDU 5623 KK's Number (博弈DP)
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即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 55000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
int num[maxn];
int dp[maxn];
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
cin >> n;
for(int i=1; i<=n; i++) scanf("%d", &num[i]);
sort(num+1, num+1+n);
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; i++) {
// for(int j=1; j<=i; j++) {
// dp[i] = max(dp[i], num[j] - dp[j-1]);
// }
dp[i] = max(dp[i-1], num[i] - dp[i-1]);
}
printf("%d\n", dp[n]);
}
return 0;
}
HDU 5623 KK's Number (博弈DP)的更多相关文章
- hdu 5623 KK's Number(dp)
问题描述 我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1≤N≤5∗104)个数,每次KK都会先拿数.每 ...
- 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 ...
- HDU 3469 Catching the Thief (博弈 + DP递推)
Catching the Thief Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU Problem D [ Humble number ]——基础DP丑数序列
Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- 博弈dp入门 POJ - 1678 HDU - 4597
本来博弈还没怎么搞懂,又和dp搞上了,哇,这真是冰火两重天,爽哉妙哉. 我自己的理解就是,博弈dp有点像对抗搜索的意思,但并不是对抗搜索,因为它是像博弈一样,大多数以当前的操作者来dp,光想是想不通的 ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
随机推荐
- bash: ./device/nexell/tools/build.sh: 权限不够
/bin/bash: build/tools/diff_package_overlays.py: 鏉冮檺涓嶅 i686-linux-gcc: error trying to exec 'cc1': ...
- Android通过JNI调用驱动程序(完全解析实例)
要达到的目的:android系统中,用JAVA写界面程序,调用jni中间库提供的接口,去操作某个驱动节点,实现read,writer ioctl等操作!这对底层驱动开发人员是很重要的一个调试通道,也是 ...
- hdu 2372 El Dorado (dp)
题目链接 题意:给n个数字, 求有k个数字的上升子序列有多少种. 思路:d[i][j]表示 以第i个元素为 子序列的最后一个元素,长度为j的子序列 有多少种. 比赛的时候 光想着用组合数做了..... ...
- IIS与ASP.NET中的队列
一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...
- POJ 1088 滑雪【记忆化搜索】
题意:给出一个二维矩阵,要求从其中的一点出发,并且当前点的值总是比下一点的值大,求最长路径 记忆化搜索,首先将d数组初始化为0,该点能够到达的路径长度保存在d数组中,同时把因为路径是非负的,所以如果已 ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- codevs 1218 疫情控制
啊好烦这道题.... 基本思路网上都有. 注意的一点是在匹配的时候,如果有军队的来源没有被匹配到,那么就先匹配这个来源.(因为不花钱). 不过数据好水.... #include<iostream ...
- ionic cordova plugin for ios
源代码结构目录: payplugin: |_src |_android |_PayPlugin.java |_ios |_CDVPayPlugin.h |_CDVPayPlugin.m |_www | ...
- ti processor sdk linux am335x evm /bin/setup-package-install.sh hacking
#!/bin/sh # # ti processor sdk linux am335x evm /bin/setup-package-install.sh hacking # 说明: # 本文主要对T ...
- django - get_or_create() 使用提醒
[omron - debug] user_id建表的时候,不能使用unique,因为一个用户,可能有多个product_id,相对应的是,get_or_create()中的查询参数,如果在建表中有un ...