Free Goodies - UVa 12260 dp

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

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

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]);
    }
}



内容概要:本文档详细介绍了基于MATLAB实现的多头长短期记忆网络(MH-LSTM)结合Transformer编码器进行多变量时间序列预测的项目实例。项目旨在通过融合MH-LSTM对时序动态的细致学习和Transformer对全局依赖的捕捉,显著提升多变量时间序列预测的精度和稳定性。文档涵盖了从项目背景、目标意义、挑战与解决方案、模型架构及代码示例,到具体的应用领域、部署与应用、未来改进方向等方面的全面内容。项目不仅展示了技术实现细节,还提供了从数据预处理、模型构建与训练到性能评估的全流程指导。 适合人群:具备一定编程基础,特别是熟悉MATLAB和深度学习基础知识的研发人员、数据科学家以及从事时间序列预测研究的专业人士。 使用场景及目标:①深入理解MH-LSTM与Transformer结合的多变量时间序列预测模型原理;②掌握MATLAB环境下复杂神经网络的搭建、训练及优化技巧;③应用于金融风险管理、智能电网负荷预测、气象预报、交通流量预测、工业设备健康监测、医疗数据分析、供应链需求预测等多个实际场景,以提高预测精度和决策质量。 阅读建议:此资源不仅适用于希望深入了解多变量时间序列预测技术的读者,也适合希望通过MATLAB实现复杂深度学习模型的开发者。建议读者在学习过程中结合提供的代码示例进行实践操作,并关注模型训练中的关键步骤和超参数调优策略,以便更好地应用于实际项目中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值