POJ1015

问题重述:

在n个候选者中选取m个人进入陪审团。每个候选者获得两项评分:D[j],P[j]。求解最佳评审团,使得在每个成员的两项评分和之差 最小的情况下,使得两项评分和之和 最大。

分析:

欲采用动态规划求解,必须先找到最优子结构。假如考虑评分差的绝对值,它的子问题并不一定是最优解。若考虑一定评分差下的评分和最大值,则拥有最优子结构。

用dp[i][j]表示在第i个评委评分后,评分差是j的最大评分和,得到递归公式:

dp[i][j] = max{ dp[ i - 1 ][ j - (D[i] - P[j]) ] + D[i] + P[j], dp[ i - 1 ][j] }

AC代码

 //Memory: 380K        Time: 79MS
 #include <iostream>
 #include <cstring>
 #include <cstdio>
 #include <algorithm>

 using namespace std;

 ][];
 ][];
 int n, m;
 ], d[];
 ;
 ];

 bool findPath(int n, int s, int target)
 {
     ) return false;
     if (target == prior[n][s + offset]) return true;
     int p = prior[n][s + offset];
     , s - (d[p] - q[p]), target);
 }

 void output(int ans)
 {
     ; j--) {
         r[j - ] = prior[j][ans + offset];
         int p = prior[j][ans + offset];
         ans -= (d[p] - q[p]);
     }
     sort(r, r + m);
     ; i < m; i++)
         cout << " " << r[i];
     cout << endl << endl;
 }

 int main()
 {
     ;
     while ( cin >> n >> m && n ) {
         ; i <= n; i++)
             cin >> d[i] >> q[i];
         memset(dp, -, sizeof(dp));
         memset(prior, , sizeof(prior));
         dp[][offset] = ;

         ; i <= m; i++) {
             ; j <= n; j++) {
                 int ss = d[j] + q[j];
                 int dd = d[j] - q[j];
                 ; s <= ; s++) {
                      || s - dd > ) continue;
                     ][s - dd + offset] != - && dp[i - ][s - dd + offset] + ss > dp[i][s + offset] && !findPath(i - , s - dd, j)) {
                         dp[i][s + offset] = dp[i - ][s - dd + offset] + ss;
                         prior[i][s + offset] = j;
                     }
                 }
             }
         }
         ; i <= ; i++) {
              && dp[m][offset - i] == -) continue;

             int ans = dp[m][offset + i] > dp[m][offset - i] ? i : -i;

             ;
             ;

             cout << "Jury #" << cas++ << endl;
             cout << "Best jury has value " << sd << " for prosecution and value "
                 << sq << " for defence:" << endl;
             output(ans);
             break;
         }
     }
     ;
 }

POJ1015 动态规划的更多相关文章

  1. [POJ1015]Jury Compromise

    题目大意:要求你从n个人中选出m个,每个人有两个值p[i],D[i],要求选出的人p总和与D总和的差值最小.若有相同解,则输出p总+D总最大的方案. 动态规划. 一直在想到底是n枚举外面还是m放外面, ...

  2. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  3. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  4. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  5. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  6. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  7. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  8. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  9. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

随机推荐

  1. ubuntu "mkdir -p"命令

    mkdir的-p选项允许你一次性创建多层次的目录,而不是一次只创建单独的目录.例如,我们要在当前目录创建目录Projects/a/src,使用命令: mkdir -p Project/a/src 而不 ...

  2. The 2015 China Collegiate Programming Contest Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. poj 2251 Dungeon Master(bfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  4. python 标准库基础学习之开发工具部分1学习

    #2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...

  5. python glob标准库基础学习

    #glob文件名模式匹配#作用:使用unix shell规则查找与一个模式匹配文件名"""尽管glob api很小,但这个模块很强大,只要程序需要查找文件系统中名字与某种 ...

  6. python中os模块常用方法

    #!/usr/bin/python## os module test import os print 'os.name: ', os.nameprint 'os.getcwd(): ', os.get ...

  7. (转)iPhone 判断UITableView 滚动到底部

    UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate. ...

  8. JAVA学习第三十课(经常使用对象API)- String类:类方法练习

    intern方法 public class Main { public static void main(String[] args) { String str1 = new String(" ...

  9. C/C++变量命名规则

    变量命名规则是为了增强代码的可读性和easy维护性. 变量命名规则: 一.用最短字符表示最准确的意义. 二.使用变量前缀. 1.  整型前缀 int               nId;        ...

  10. crtmpserver通常使用基本类演示

    以前我们做了分析过程,这一次,我们都参与了类做梳子,两个可以一起关注一下一起合并,整个方案的实施是有帮助. BaseClientApplication APP基类,一切APP都基于这个类 Stream ...