文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>poj1015[背包]

poj1015[背包]

时间:2010-09-29  来源:wbh23

Jury Compromise
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15380   Accepted: 3968   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.

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.

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.

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. 背包式的dp

 

代码
 1 #include <cstdio>
2 #include <cstring>
3 #include <vector>
4 #include <algorithm>
5 using namespace std;
6
7 #define maxn 220
8 #define offset 25*20
9 pair <int , int> cdd[maxn];
10 int dp[22][400+offset];
11 int par[22][400+offset];
12 vector <int> order;
13
14 int cmp(int a,int b){return a>b;}
15 /*candidate[a] exists in i men*/
16 int notused(int a , int i, int p){
17 while(par[i][p] != -1)
18 {
19 if( par[i][p] == a)return 0;
20 p = p - cdd[par[i][p]].first + cdd[par[i][p]].second;
21 i--;
22 }
23 return 1;
24 }
25
26 int main(){
27 int i, j , k , p ,n , m ,ncase=0;
28 while( scanf("%d %d",&n,&m) != EOF && m)
29 {
30 memset(dp , -1 , sizeof(dp));
31 memset(par , 0 , sizeof(par));
32 dp[0][offset] = 0;
33 par[0][offset] = -1;
34 for(i=0 ; i<n ; i++)
35 scanf("%d %d",&cdd[i].first,&cdd[i].second);
36
37 /* dp[j][p] = max( dp[j-1][p+first[i]-second[i]] + first[i] + second[i]) */
38 for(i=1 ; i<=m ; i++)
39 { /* 01 */
40 for(p=0 ; p<400+offset ; p++)
41 {
42 if( dp[i-1][p] != -1)
43 {
44 for(j=0 ; j<n ; j++)
45 {
46 if(notused(j,i-1,p) && dp[i-1][p]+cdd[j].first+cdd[j].second > dp[i][p+cdd[j].first-cdd[j].second])
47 {
48 dp[i][ p+cdd[j].first-cdd[j].second ] = dp[i-1][p]+cdd[j].first+cdd[j].second;
49 par[i][ p + cdd[j].first - cdd[j].second ] = j;
50 }
51 }
52 }//if
53 }
54 }/* for(i)*/
55
56 for( k=0 ; k<=20*m ; k++)
57 {
58 if(dp[m][offset+k]!=-1 || dp[m][offset-k]!=-1 )
59 break;
60 }
61 if( dp[m][offset-k] > dp[m][offset+k]) k = -k;
62
63 p = offset+k; i=m;
64 order.clear();
65 while(par[i][p] != -1)
66 {
67 order.push_back(par[i][p]);
68 p = p - cdd[par[i][p]].first + cdd[par[i][p]].second;
69 i--;
70 }
71 pair <int,int> count=make_pair(0,0);
72 for(i=0 ; i<order.size() ; i++)
73 {
74 count.first += cdd[order[i]].first;
75 count.second += cdd[order[i]].second;
76 }
77 printf("Jury #%d\nBest jury has value %d for prosecution and value %d for defence:\n",++ncase , count.first,count.second);
78 sort(order.begin(),order.end(),cmp);
79 for(i=order.size()-1 ; i>=0 ; i--)printf(" %d",order[i]+1);
80 printf("\n\n");
81
82 }
83 return 0;
84 }

 

 

相关阅读 更多 +
排行榜 更多 +
火柴人战争血腥打击

火柴人战争血腥打击

飞行射击 下载
沉浸式射击比赛

沉浸式射击比赛

飞行射击 下载
银河世界

银河世界

飞行射击 下载