hdoj 5623 KK's Number 【dp】

KK's Number

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


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
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个数,现在两个人轮流选数,每次可以选任意多个数但不能不取,选出的所有数中最小的即为每次获得分数。两人都足够聪明,问最后的结果——先手 - 后手的分差的最小值。


思路:每次选的可以使较大的数。那么可以先sort一下。设置dp[i]为先手取a[i] - a[n]的最优状态。

倒着来做dp,dp[i] = a[i] - max(dp[j]) (1 <= j < i),ans = max(dp[i]) (1 <= i <= n)。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 1000000
#define eps 1e-8
#define MAXN (50000+10)
#define MAXM (10000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int a[MAXN], dp[MAXN];
int main()
{
    int t; Ri(t);
    W(t)
    {
        int n; Ri(n);
        for(int i = 1; i <= n; i++) Ri(a[i]);
        sort(a+1, a+n+1); int Max = 0;
        for(int i = 1; i <= n; i++)
        {
            dp[i] = a[i] - Max;
            Max = max(Max, dp[i]);
        }
        Pi(Max);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值