http://poj.org/problem?id=1015

题目大意:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定。陪审团是由法官从公众中挑选的。先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团。选m人的办法是:控方和辩方会根据对候选人的喜欢程度,给所有候选人打分,分值从0到20。为了公平起见,法官选出陪审团的原则是:选出的m个人,必须满足辩方总分和控方总分的差的绝对值最小。如果有多种选择方案的辩方总分和控方总分的之差的绝对值相同,那么选辩控双方总分之和最大的方案即可。

其实学DP首先要从背包开始,学了背包后,看这题就是一个背包 + 记录路径的问题

设dp[m][k] = max表示,选了m个数,产生的和差值是k的时候,的最大和值。

但是,这个背包是不能记录路径的,

因为

2 2

1 2

3 4

这组数据,生成和差值是-1的时候有两个,这样会存在路径覆盖问题。

所以要这样做,暴力枚举m组,对于每一次,都在1---n中选一个,然后选的时候看看是否存在于这条路径就行了。

也就是对于上面的数据,一开始选解出选出1个的时候的最优值,那自然是选第二个数,然后,再解出选出两个数的时候的最优解。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n, m;
const int fix = ;
const int maxn = + ;
struct node {
int b, c, dis, sum;
}a[maxn];
const int up = * + ;
struct DP {
int is, pre;
int id, val;
}dp[ + ][up];
set<int>ans;
bool in(int go, int val, int cmp) {
while (go > ) {
if (dp[go][val].is == -) return false;
if (dp[go][val].id == cmp) return true;
val = dp[go][val].pre;
assert(val <= up);
assert(val >= );
go--;
}
return false;
}
void work() {
// init();
for (int i = ; i <= n; ++i) {
scanf("%d%d", &a[i].b, &a[i].c);
a[i].dis = a[i].b - a[i].c + fix;
a[i].sum = a[i].b + a[i].c;
}
memset(dp, -, sizeof dp);
dp[][].id = inf, dp[][].is = , dp[][].pre = inf, dp[][].val = ;
for (int i = ; i <= m; ++i) { //选m个
for (int j = ; j <= n; ++j) {
for (int k = a[j].dis; k <= up - ; ++k) {
if (dp[i - ][k - a[j].dis].is == -) continue;
if (dp[i][k].val < dp[i - ][k - a[j].dis].val + a[j].sum && !in(i - , k - a[j].dis, j)) {
// if (dp[i][k].id == j) continue;
dp[i][k].val = dp[i - ][k - a[j].dis].val + a[j].sum;
dp[i][k].is = ;
dp[i][k].id = j;
dp[i][k].pre = k - a[j].dis;
}
}
}
}
int p1 = m * fix, p2 = m * fix;
int id1 = -inf, id2 = -inf, idans = -inf;
while (true) {
if (dp[m][p1].is != -) id1 = p1;
if (dp[m][p2].is != -) id2 = p2;
if (id1 != -inf && id2 == -inf) {
idans = id1;
break;
} else if (id1 == -inf && id2 != -inf) {
idans = id2;
break;
} else if (id1 != -inf && id2 != -inf) {
if (dp[m][id1].val > dp[m][id2].val) {
idans = id1;
} else idans = id2;
break;
}
p1--;
p2++;
}
// cout << idans << endl;
ans.clear();
int go = m;
while (dp[go][idans].id != inf) {
assert(dp[go][idans].is != -);
ans.insert(dp[go][idans].id);
idans = dp[go][idans].pre;
go--;
}
static int f = ;
printf("Jury #%d\n", ++f);
int ans1 = , ans2 = ;
for (set<int> :: iterator it = ans.begin(); it != ans.end(); ++it) {
ans1 += a[*it].b;
ans2 += a[*it].c;
}
// cout << endl;
printf("Best jury has value %d for prosecution and value %d for defence:\n", ans1, ans2);
for (set<int> :: iterator it = ans.begin(); it != ans.end(); ++it) {
cout << " " << *it;
}
cout << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF && (n + m)) {
work();
printf("\n");
}
return ;
}

POJ 1015 Jury Compromise 2个月后重做,其实这是背包题目的更多相关文章

  1. 背包系列练习及总结(hud 2602 && hdu 2844 Coins && hdu 2159 && poj 1170 Shopping Offers && hdu 3092 Least common multiple && poj 1015 Jury Compromise)

    作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢htt ...

  2. OpenJudge 2979 陪审团的人选 / Poj 1015 Jury Compromise

    1.链接地址: http://bailian.openjudge.cn/practice/2979 http://poj.org/problem?id=1015 2.题目: 总Time Limit: ...

  3. POJ 1015 Jury Compromise(双塔dp)

    Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33737   Accepted: 9109 ...

  4. poj 1015 Jury Compromise(背包+方案输出)

    \(Jury Compromise\) \(solution:\) 这道题很有意思,它的状态设得很...奇怪.但是它的数据范围实在是太暴露了.虽然当时还是想了好久好久,出题人设了几个限制(首先要两个的 ...

  5. POJ 1015 Jury Compromise dp分组

    第一次做dp分组的问题,百度的~~ http://poj.org/problem?id=1015 题目大意:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑 ...

  6. [Poj 1015] Jury Compromise 解题报告 (完全背包)

    题目链接:http://poj.org/problem?id=1015 题目: 题解: 我们考虑设计DP状态(因为这很显然是一个完全背包问题不是吗?) dp[j][k]表示在外层循环到i时,选了j个人 ...

  7. POJ #1015 - Jury Compromise - TODO: POJ website issue

    (poj.org issue. Not submitted yet) This is a 2D DP problem, very classic too. Since I'm just learnin ...

  8. POJ 1015 Jury Compromise(dp坑)

    提议:在遥远的国家佛罗布尼亚,嫌犯是否有罪,须由陪审团决定.陪审团是由法官从公众中挑选的.先随机挑选n个人作为陪审团的候选人,然后再从这n个人中选m人组成陪审团.选m人的办法是:控方和辩方会根据对候选 ...

  9. POJ 1015 Jury Compromise (记录路径的背包问题)

    (点击此处查看原题) 题意 为了审判某一个人,需要在n个人当中选出m个人组成陪审团,n个人中每个人都有作为起诉方的价值p和作为辩护方的价值d,为了保证公平性,要求m个人作为起诉方的价值之和P和作为辩护 ...

随机推荐

  1. WORD的公式无法与文字对齐

    在使用Mathtype编辑公式后,经常出现以下公式与文字无法对齐的问题: 可以使用以下方式来解决:

  2. Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.

    问题: Azure Sql 在插入数据是出现“Msg 40054, Level 16, State 1, Line 2  Tables without a clustered index are no ...

  3. w3svc服务启动 不了,错误 1068:依赖服务或组件无法启动

    win10系统,装了iis就是启动不了,报错误 1068:依赖服务或组件无法启动. 各种实验无法使用,最后如下方法解决 运行命令regedit,打开注册表编辑器,进入:HKEY_LOCAL_MACHI ...

  4. get application power

    1. http://blog.csdn.net/sjz_iron/article/details/8726661 http://www.16rd.com/home.php?mod=space& ...

  5. linux journel

    http://www.linuxjournal.com/article/8545 http://www.linuxjournal.com/article/8093 http://www.linuxjo ...

  6. c# 文件及目录操作类

    18位长度的计时周期数: DateTime.Now.Ticks.ToString() 多数是收集而来,加上测试感觉很不错,分享一下或许有些帮助吧: 引用: using System; using Sy ...

  7. 慕课网-Java入门第一季-6-7 使用 Arrays 类操作 Java 中的数组

    来源:http://www.imooc.com/code/1556 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现 ...

  8. 对于有了ACM以后的生活

    我是大二学生,才接触ACM不到5个星期,因为受到我们班dalao的"引诱",去参加了一次我们学校举行的萌新杯,于是就入坑了,而我又在校外学习一些关于安全的知识,前几天一直纠结要不要 ...

  9. UVA 11809 - Floating-Point Numbers

    数学太渣了,这道题反复参考了大神的博客,算是看懂了吧.博客原文  http://blog.csdn.net/crazysillynerd/article/details/43339157 算是个数学题 ...

  10. JS中Array详细用法

    1.数组的创建 var name= new Array(); //创建一个数组 name[0]="zhangsan";   //给数组赋值 name[1]="lisi&q ...