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



这个是完整源码 python实现 Flask,Vue 【python毕业设计】基于Python的Flask+Vue物业管理系统 源码+论文+sql脚本 完整版 数据库是mysql 本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发
### 下载 nginx-goodies-nginx-sticky-module 模块 nginx-goodies-nginx-sticky-module 是一个第三方模块,用于实现基于 Cookie 的会话保持功能。该模块并不是 Nginx 官方提供的模块,而是由社区开发和维护的。用户可以通过克隆其 Git 仓库来获取该模块的源代码。 具体的下载方式如下: ```bash cd /k8s-deploy git clone https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng.git ``` 上述命令会将模块源代码克隆到本地目录中,以便后续在编译 Nginx 时添加该模块[^3]。 ### 编译 Nginx 时添加模块 在成功下载 nginx-sticky-module 模块后,需要在编译 Nginx 时通过 `--add-module` 参数指定模块路径。例如: ```bash ./configure --prefix=/usr/local/nginx --add-module=../nginx-sticky-module-ng ``` 此步骤确保 Nginx 在编译过程中包含该第三方模块,从而启用基于 Cookie 的会话保持功能[^1]。 需要注意的是,nginx-sticky-module 依赖于 Nginx 的源码,因此在编译时还需要确保已经下载了对应版本的 Nginx 源代码。例如,在克隆 Nginx 源码后,进入其目录并执行配置命令: ```bash cd /k8s-deploy/nginx-1.25.0 ./configure --add-module=../nginx-sticky-module-ng make && sudo make install ``` 上述步骤完成后,Nginx 将支持 `sticky` 模块,并可在配置文件中使用 `sticky` 指令来实现会话保持功能[^3]。 ### 配置示例 在 Nginx 配置文件中,可以使用如下方式启用基于 Cookie 的会话保持: ```nginx upstream backend { sticky name=ngx_cookie expires=6h; server 192.168.31.240:8080 weight=3 max_fails=3 fail_timeout=10s; server 192.168.31.241:8080 weight=3 max_fails=3 fail_timeout=10s; server 192.168.31.242:8080 weight=6 max_fails=3 fail_timeout=10s; server 192.168.31.243:8080; server 192.168.31.244:8080 down; } ``` 该配置通过 `sticky` 指令定义了基于 Cookie 的会话保持策略,确保客户端请求始终被转发到同一台后端服务器[^4]。 ### 注意事项 1. nginx-sticky-module 仅适用于开源版本的 Nginx,而 Nginx Plus 已经内置了类似的会话保持功能。 2. 在使用该模块时,需要确保其版本与 Nginx 的版本兼容。 3. 编译完成后,可以通过 `nginx -V` 命令检查是否成功添加了模块。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值