uva12260 - Free Goodies 贪心+dp

本文介绍了一种通过轮流选择方式公平分配不同价值糖果的算法。Petra 和 Jan 按照特定策略选择糖果,确保双方获得的价值尽可能公平。文章详细解释了实现这一目标的具体步骤和考虑因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B  Free Goodies

Petra and Jan have just received a box full of free goodies, and want to divide the goodies between them. However, it is not easy to do this fairly, since they both value different goodies differently.To divide the goodies, they have decided upon the following procedure: they choose goodies one by one, in turn, until all the goodies are chosen. A coin is tossed to decide who gets to choose the first goodie.Petra and Jan have different strategies in deciding what to choose. When faced with a choice, Petra always selects the goodie that is most valuable to her. In case of a tie, she is very considerate and picks the one that is least valuable to Jan. (Since Petra and Jan are good friends, they know exactly how much value the other places on each goodie.)Jan's strategy, however, consists of maximizing his own final value. He is also very considerate, so if multiple choices lead to the same optimal result, he prefers Petra to have as much final value as possible.You are given the result of the initial coin toss. After Jan and Petra have finished dividing all the goodies between themselves, what is the total value of the goodies each of them ends up with?

Input

On the first line a positive integer: the number of test cases, at most 100. After that per test case:
  • One line with an integer n (1 ≤ n ≤ 1 000): the number of goodies.
  • One line with a string, either "Petra" or "Jan": the person that chooses first.
  • n lines with two integers pi and ji (0 ≤ pi,ji ≤ 1 000) each: the values that Petra and Jan assign to the i-th goodie, respectively.

Output

Per test case:
  • One line with two integers: the value Petra gets and the value Jan gets. Both values must be according to their own valuations.

Sample in- and output

InputOutput
3
4
Petra
100 80
70 80
50 80
30 50
4
Petra
10 1
1 10
6 6
4 4
7
Jan
4 1
3 1
2 1
1 1
1 2
1 3
1 4
170 130
14 16
9 10

  两个人轮流拿糖果,对于某个糖果如果petra拿了得到权值x,jan拿了得到权值y。petra的选择是拿当前x最大的,如果有多个拿y最小的,jan的选择是使他最后得到的权值最多,在他得到最多的情况下使petra也尽量多。这题参考了题解。

  把这些糖果按x从大到小排序,x相同时y从小到大。那么petra一定是按照这个顺序拿,如果petra先拿,那么前n个里面jan最多拿n/2个,用dp[i][j]表示jan前i个里面拿j个,j<=i/2,那么dp[i][j]=min(dp[i-1][j],dp[i-1][j-1]+y[i] | 当dp[i-1][j-1]存在),dp2[i][j]表示前i个里面jan拿掉j个petra失去的权值,越小越好,在维护dp的时候同时维护dp2就行了。

  注意如果是jan先,那么前i个他可以拿掉(i+1)/2个。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

typedef long long LL;

const int MAXN=1010;
const int MAXNODE=MAXN*4;
const LL MOD=1000000007;

int T,N;
int dp[MAXN][MAXN/2],dp2[MAXN][MAXN/2];
char str[10];

struct St{
    int x,y;
    bool operator < (const St& rhs) const{
        if(x==rhs.x) return y<rhs.y;
        return x>rhs.x;
    }
}a[MAXN];

int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%s",&N,str);
        int sum=0;
        for(int i=1;i<=N;i++){
            scanf("%d%d",&a[i].x,&a[i].y);
            sum+=a[i].x;
        }
        sort(a+1,a+1+N);
        memset(dp,-1,sizeof(dp));
        memset(dp2,0,sizeof(dp2));
        for(int i=0;i<=N;i++) dp[i][0]=0;
        int n=str[0]=='P'?0:1;
        for(int i=1;i<=N;i++){
            n++;
            for(int j=1;j<=n/2;j++){
                dp[i][j]=dp[i-1][j];
                dp2[i][j]=dp2[i-1][j];
                if(dp[i-1][j-1]!=-1){
                    if(dp[i-1][j-1]+a[i].y>dp[i][j]){
                        dp[i][j]=dp[i-1][j-1]+a[i].y;
                        dp2[i][j]=dp2[i-1][j-1]+a[i].x;
                    }
                    else if(dp[i-1][j-1]+a[i].y==dp[i][j]&&dp2[i-1][j-1]+a[i].x<dp2[i][j]) dp2[i][j]=dp2[i-1][j-1]+a[i].x;
                }
            }
        }
        printf("%d %d\n",sum-dp2[N][n/2],dp[N][n/2]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值