POJ 1015
来源:互联网 发布:华为路由器nat端口复用 编辑:程序博客网 时间:2024/05/16 08:56
Jury Compromise
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23258 Accepted: 6012 Special Judge
Description
In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is done as follows. First, several people are drawn randomly from the public. For each person in this pool, defence and prosecution assign a grade from 0 to 20 indicating their preference for this person. 0 means total dislike, 20 on the other hand means that this person is considered ideally suited for the jury.
Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties.
We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to J
and P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution.
For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties.
You are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.
Based on the grades of the two parties, the judge selects the jury. In order to ensure a fair trial, the tendencies of the jury to favour either defence or prosecution should be as balanced as possible. The jury therefore has to be chosen in a way that is satisfactory to both parties.
We will now make this more precise: given a pool of n potential jurors and two values di (the defence's value) and pi (the prosecution's value) for each potential juror i, you are to select a jury of m persons. If J is a subset of {1,..., n} with m elements, then D(J ) = sum(dk) k belong to J
and P(J) = sum(pk) k belong to J are the total values of this jury for defence and prosecution.
For an optimal jury J , the value |D(J) - P(J)| must be minimal. If there are several jurys with minimal |D(J) - P(J)|, one which maximizes D(J) + P(J) should be selected since the jury should be as ideal as possible for both parties.
You are to write a program that implements this jury selection process and chooses an optimal jury given a set of candidates.
Input
The input file contains several jury selection rounds. Each round starts with a line containing two integers n and m. n is the number of candidates and m the number of jury members.
These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next.
The file ends with a round that has n = m = 0.
These values will satisfy 1<=n<=200, 1<=m<=20 and of course m<=n. The following n lines contain the two integers pi and di for i = 1,...,n. A blank line separates each round from the next.
The file ends with a round that has n = m = 0.
Output
For each round output a line containing the number of the jury selection round ('Jury #1', 'Jury #2', etc.).
On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number.
Output an empty line after each test case.
On the next line print the values D(J ) and P (J ) of your jury as shown below and on another line print the numbers of the m chosen candidates in ascending order. Output a blank before each individual candidate number.
Output an empty line after each test case.
Sample Input
4 2 1 2 2 3 4 1 6 2 0 0
Sample Output
Jury #1 Best jury has value 6 for prosecution and value 4 for defence: 2 3
Hint
If your solution is based on an inefficient algorithm, it may not execute in the allotted time.
Source
Southwestern European Regional Contest 1996
参照网上一位大神的思路:
题目大意
这是一道很不错的动态规划DP题,题目大意是有n组数据,每组数据有两个属性,从中选取m个数据,使得属性一之和和属性二之和的差最小,如果同时存在多组差相同的,则选择属性一之和和属性二之和的和最大的。同时要记录选取的数据集。
解题思路
这是一道很好的动态规划题目,要求记录中间的过程。我用了两种方法做了下。
第一种方法:
dp[i][j][k]表示前i个数据中选取j个数据两种属性差为k时的两种属性和。
dp[i][j][k]=max(dp[i-1][j][k],dp[i-1][j-1][k-d[i]]+p[i])
这样用path[i][j][k]表示对应的轨迹,如果使用了第i个数据,则path[i][j][k]=I,这样前一个数据就是path[i-1][j-1][k-d[i]],以此类推下去就可以得到全部的轨迹。
第二种方法:
dp[j][k]表示选取j个数据属性差为k时的属性和
dp[j][k]=max(dp[j-1][k-d[i]]),其中i=1,2,3,…n
这个时候可以用path[j][k]=i表示对应的路径,path[j][k]的上一个路径就是path[j-1][k-d[path[j][k]]]
无论哪一种方法,由于差值可以为负,所以DP的差值状态要有一个便宜,20个数据最大为20,总共就是400,所以可以用400做偏移量,很多人都是这样做的,但是我的却出问题了,因为中间有一处差值从0到800时0减去了一个正的差值,会导致结果为负,所以我就用了420,这点应该是可以改进的。
这是一道很不错的动态规划DP题,题目大意是有n组数据,每组数据有两个属性,从中选取m个数据,使得属性一之和和属性二之和的差最小,如果同时存在多组差相同的,则选择属性一之和和属性二之和的和最大的。同时要记录选取的数据集。
解题思路
这是一道很好的动态规划题目,要求记录中间的过程。我用了两种方法做了下。
第一种方法:
dp[i][j][k]表示前i个数据中选取j个数据两种属性差为k时的两种属性和。
dp[i][j][k]=max(dp[i-1][j][k],dp[i-1][j-1][k-d[i]]+p[i])
这样用path[i][j][k]表示对应的轨迹,如果使用了第i个数据,则path[i][j][k]=I,这样前一个数据就是path[i-1][j-1][k-d[i]],以此类推下去就可以得到全部的轨迹。
第二种方法:
dp[j][k]表示选取j个数据属性差为k时的属性和
dp[j][k]=max(dp[j-1][k-d[i]]),其中i=1,2,3,…n
这个时候可以用path[j][k]=i表示对应的路径,path[j][k]的上一个路径就是path[j-1][k-d[path[j][k]]]
无论哪一种方法,由于差值可以为负,所以DP的差值状态要有一个便宜,20个数据最大为20,总共就是400,所以可以用400做偏移量,很多人都是这样做的,但是我的却出问题了,因为中间有一处差值从0到800时0减去了一个正的差值,会导致结果为负,所以我就用了420,这点应该是可以改进的。
第一种思路:
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>using namespace std;//#define Online_Judge#define outstars cout << "***********************" << endl;#define clr(a,b) memset(a,b,sizeof(a))#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)const int MAXN = 100000 + 50;const int maxw = 100 + 20;const int MAXNNODE = 1000000 +10;const long long LLMAX = 0x7fffffffffffffffLL;const long long LLMIN = 0x8000000000000000LL;const int INF = 0x7fffffff;const int IMIN = 0x80000000;#define eps 1e-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;typedef pair<int , int> pi;int dp[210][25][900];int diff[210] , rst[25];int path[210][25][900];int main(){ //ios::sync_with_stdio(false);#ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);#endif // Online_Judge int n , m , a , b , k , len1 , len2, p , q , Case = 0; while(scanf("%d%d", &n , &m) , (n||m)) { printf("Jury #%d\n" , ++Case); len1 = 420 - 20 * m,len2 = 420 + 20 * m; clr(path , 0); clr(dp , -1); clr(diff , 0); scanf("%d%d" , &a , &b); diff[1] = a - b; int temp = 420 + a - b; dp[1][1][temp] = a + b; path[1][1][temp] = 1; FORR(i , 2 , n) { scanf("%d%d" , &a , &b); temp = 420 + a - b; diff[i] = a - b; FORR(k , len1 , len2)dp[i][1][k] = dp[i - 1][1][k]; if(a + b > dp[i][1][temp]) { path[i][1][temp] = i; dp[i][1][temp] = a + b; } for(int j = 2 ; j <= ( m > i ? i : m);j++) { FORR(k , len1, len2) { p = dp[i - 1][j][k] , q = dp[i - 1][j - 1][k - a + b]; if(q == -1) { dp[i][j][k] = p; } else { if(p >= q + a + b)dp[i][j][k] = p; else {dp[i][j][k] = q + a + b ; path[i][j][k] = i;} } } } } int i = 0; int ll , pro , def; while(dp[n][m][420 + i] == -1&&dp[n][m][420 - i] == -1)i++; if(dp[n][m][420 + i] > dp[n][m][420 - i]) { pro = (dp[n][m][420 + i] + i) >> 1; def = (dp[n][m][420 + i] - i) >> 1; ll = 420 + i; } else { pro = (dp[n][m][420 - i] - i) >> 1; def = (dp[n][m][420 - i] + i) >> 1; ll = 420 - i; } int j = m; while(j) { a = path[n][j][ll]; if(a) { rst[j] = a; ll -= diff[a]; j--; } n--; } printf("Best jury has value %d for prosecution and value %d for defence:\n" , pro , def); FORR(i , 1 ,m)printf(" %d",rst[i]); printf("\n"); } return 0;}
第二种思路:
#include"iostream"#include"algorithm"using namespace std;int d[201],p[201],dp[21][841],path[21][841];bool isvisited(int i,int j,int k){int a; while(i){a=path[i][j]; if(k==a)return true; i--;j-=p[a]; } return false; } int main(){int i,j,k,a,b,len1,len2,m,n,index=0;while(cin>>n>>m&&n!=0&&m!=0) {index++; cout<<"Jury #"<<index<<endl; memset(dp,-1,sizeof(dp));memset(path,0,sizeof(path)); len1=420-20*m;len2=420+20*m; for(i=1;i<=n;i++){ scanf("%d %d",&a,&b);d[i]=a+b;p[i]=a-b; if(dp[1][a-b+420]<a+b){dp[1][a-b+420]=a+b;path[1][a-b+420]=i;} }for(i=2;i<=m;i++){for(j=len1;j<=len2;j++){for(k=1;k<=n;k++) { if(dp[i-1][j-p[k]]!=-1&&dp[i][j]<dp[i-1][j-p[k]]+d[k]&&!isvisited(i-1,j-p[k],k)){dp[i][j]=dp[i-1][j-p[k]]+d[k];path[i][j]=k; } } } }int ll,rst[21],pro,def; i=0; while(dp[m][420+i]==-1&&dp[m][420-i]==-1)i++; if(dp[m][420+i]>dp[m][420-i]){pro=(dp[m][420+i]+i)/2;def=(dp[m][420+i]-i)/2;ll=420+i; } else{pro=(dp[m][420-i]-i)/2;def=(dp[m][420-i]+i)/2;ll=420-i; } j=m; while(j){a=path[j][ll];rst[j]=a; j--;ll-=p[a]; } sort(rst+1,rst+m+1); cout<<"Best jury has value "<<pro<<" for prosecution and value "<<def<<" for defence:"<<endl; for(i=1;i<=m;i++)printf(" %d",rst[i]);cout<<endl<<endl; } }
- POJ 1015
- POJ-1015
- POJ 1015
- POJ 1015
- POJ 1015
- Poj 1015
- poj-1015
- poj 1015
- POJ 1015
- POJ 1015
- POJ 1015
- POJ 1015
- poj 1015
- POJ 1015题意
- POJ 1015 Jury Compromise
- poj 1015 Jury Compromise
- [poj]动态规划1015
- POJ 1015 Jury Compromise
- 启用MFC的CClientDC类实现画线功能
- Opencv_ORB
- GIS地图开发之地图交互操作(增加、删除要素)与要素信息显示
- HDU-4649 dp压缩
- Windows 7中怎样找到真正的Administrator账户
- POJ 1015
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- JS实现日历控件
- 1004 Let the Balloon Rise
- python小练习
- 回首这段程序员的日子
- java生产者消费者问题实现
- java数据类型
- 只能输入数据或英文 ,很笨的方法