hdoj KK's Number 5623 (DP)

来源:互联网 发布:迅捷网络官方登录网站 编辑:程序博客网 时间:2024/05/22 01:27

KK's Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 214    Accepted Submission(s): 128


Problem Description
Our lovely KK has a funny mathematical game:This game requires two people,There are N(1N5104) numbers,every time KK will take the numbers,first.Every time you can take any number of the numbers.Until the N 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 T(1T10), which indicates the number of test cases.
For each test case, there are two lines,in the first line is a integer N(1N5104),the other line has N positive integers(no more than 109).
 

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
131 3 1
 

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

KK's Number

 
 Accepts: 59
 
 Submissions: 137
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
我们可爱的KK有一个有趣的数学游戏:这个游戏需要两个人,有N\left(1\leq N\leq 5*{10}^{4} \right)N(1N5104)个数,每次KK都会先拿数。每次可以拿任意多个数,直到NN个数被拿完。每次获得的得分为取的数中的最小值,KK和对手的策略都是尽可能使得自己的得分减去对手的得分更大。在这样的情况下,最终KK的得分减去对手的得分会是多少?
输入描述
第一行一个数T\left( 1\leq T\leq 10\right)T(1T10),表示数据组数。对于每组数据包含两行,第一行一个整数N\left(1\leq N\leq 5*{10}^{4} \right)N(1N5104),表示个数,第二行NN个正整数(不超过{10}^{9}109)。
输出描述
对于每一个数据输出一个整数,表示最终KK的得分减去对手的得分。
输入样例
131 3 1
输出样例
2
Hint
首先KK取走3,然后对手取走两个1,那么最终分差为2。
//思路:
因为他们的得分都是他们取得的数中的最小的,所以取的分的个数不在于多。先将n个数排序,计算出两个数之间的最大间隔m,那么这个m即为所求。
Hint:得有特殊判断,如果m小于a[0]的话就输出a[0](表示KK将所有数都拿走,对手分数为0)。(在这块WA了两次)。。。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[50010];int main(){int t,i,j,n,m,k;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);int m,ma=0;for(i=0;i<n;i++){m=a[i]-ma;ma=max(ma,m);}printf("%d\n",ma);}return 0;}

0 0
原创粉丝点击