Free Goodies - UVa 12260 dp

本博客探讨了Petra和Jan如何通过一种策略公平地分配不同价值的糖果,Petra追求个人最大价值,而Jan则在最大化自身最终价值的同时考虑到Petra的利益。通过详细的步骤和示例输入输出,展示了如何解决此类问题,旨在理解在有限资源分配中平衡双方利益的方法。

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每次会取当前对自己价值最大的,如果有多解,取对Jan价值最小的,Jan每次会采取对自己最后的价值最大的方式,如果有多解,取对Petra最后结果价值最大的,问最后取的结果。

思路:先按照Petra取的顺序排序,然后dp[i][j]表示前i个硬币中Petra取走j个的时候,两个人当前硬币的情况,然后转移时考虑某些非法情况,不能转移。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int a,b;
}coin[1010];
bool cmp(node a,node b)
{
    if(a.a==b.a)
      return a.b<b.b;
    return a.a>b.a;
}
char s[20];
int dp[2][1010][2];
int main()
{
    int T,t,n,i,j,k,a,b;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&n);
        scanf("%s",s);
        if(s[0]=='P')
          k=0;
        else
          k=1;
        for(i=1;i<=n;i++)
           scanf("%d%d",&coin[i].a,&coin[i].b);
        sort(coin+1,coin+1+n,cmp);
        for(i=0;i<=n;i++)
           dp[0][i][0]=-1;
        dp[0][0][0]=0;dp[0][0][1]=0;
        for(i=1;i<=n;i++)
        {
            if(i&1)
              a=0,b=1;
            else
              a=1,b=0;
            for(j=0;j<=n;j++)
              dp[b][j][0]=-1;
            for(j=0;j<=i;j++)
               if(i-j<=j+k)
               {
                   if(dp[a][j][0]==-1 && (j==0 || dp[a][j-1][0]==-1))
                      continue;
                   if(dp[a][j][0]==-1)
                   {
                       dp[b][j][0]=dp[a][j-1][0]+coin[i].a;
                       dp[b][j][1]=dp[a][j-1][1];
                   }
                   else if(j==0 || dp[a][j-1][0]==-1)
                   {
                       dp[b][j][0]=dp[a][j][0];
                       dp[b][j][1]=dp[a][j][1]+coin[i].b;
                   }
                   else if(dp[a][j-1][1]>dp[a][j][1]+coin[i].b ||
                           (dp[a][j-1][1]==dp[a][j][1]+coin[i].b && dp[a][j-1][0]+coin[i].a>=dp[a][j][0])
                           )
                   {
                       dp[b][j][0]=dp[a][j-1][0]+coin[i].a;
                       dp[b][j][1]=dp[a][j-1][1];
                   }
                    else
                   {
                       dp[b][j][0]=dp[a][j][0];
                       dp[b][j][1]=dp[a][j][1]+coin[i].b;
                   }
                   //printf("%d %d %d %d\n",i,j,dp[b][j][0],dp[b][j][1]);
               }
        }
        if(n&1)
        {
            if(k==0)
              k=n/2+1;
            else
              k=n/2;
        }
        else
          k=n/2;
        printf("%d %d\n",dp[b][k][0],dp[b][k][1]);
    }
}



当前,全球经济格局深刻调整,数字化浪潮席卷各行各业,智能物流作为现代物流发展的必然趋势和关键支撑,正迎来前所未有的发展机遇。以人工智能、物联网、大数据、云计算、区块链等前沿信息技术的快速迭代与深度融合为驱动,智能物流不再是传统物流的简单技术叠加,而是正在经历一场从自动化向智能化、从被动响应向主动预测、从信息孤岛向全面互联的深刻变革。展望2025年,智能物流系统将不再局限于提升效率、降低成本的基本目标,而是要构建一个感知更全面、决策更精准、执行更高效、协同更顺畅的智慧运行体系。这要求我们必须超越传统思维定式,以系统化、前瞻性的视角,全面规划和实施智能物流系统的建设。本实施方案正是基于对行业发展趋势的深刻洞察和对未来需求的精准把握而制定。我们的核心目标在于:通过构建一个集成了先进感知技术、大数据分析引擎、智能决策算法和高效协同平台的综合智能物流系统,实现物流全链路的可视化、透明化和智能化管理。这不仅是技术层面的革新,更是管理模式和服务能力的全面提升。本方案旨在明确系统建设的战略方向、关键任务、技术路径和实施步骤,确保通过系统化部署,有效应对日益复杂的供应链环境,提升整体物流韧性,优化资源配置效率,降低运营成本,并最终为客户创造更卓越的价值体验。我们致力于通过本方案的实施,引领智能物流迈向更高水平,为构建现代化经济体系、推动高质量发展提供强有力的物流保障。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值