zoukankan      html  css  js  c++  java
  • POJ 1015 Jury Compromise (DP)

    题目

    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.

    题解

    首先两个值是捆绑在一起选择的,那么这个东西似乎有点类似于01背包,但是这里的不同点在于01背包已经规定了背包的容量,但是这道题目要求我们找到最小的容量。那么dp的性质肯定是最大化dp值,也就是我们的(sum_{i=1}^{cnt}(p[i]+d[i])),那么我们加一维状态表示我们当前的体积,由于这道题会出现偏移量,所以我们把范围设到0-800即可。然后我们从中值出发,往左右移动,得到最小的体积,因为其实我们取的是绝对值。然后从(dp[i][j][v])出发,根据dp值之间的关系,把路径给复原出来,最后得到最后的路径,过程中统计sum值,最后输出即可。

    代码

    #include<vector>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    const int inf=0x3f3f3f3f;
    void check_max (int &a,int b) {a=max (a,b);}
    void check_min (int &a,int b) {a=min (a,b);}
    int n,m;
    const int maxn=205,st=400,M=805;
    int d[maxn],p[maxn];
    vector <int> ans;
    int dp[maxn][21][M];
    
    void f () {
        memset (dp,-inf,sizeof (dp));
        dp[0][0][st]=0;
        ans.clear ();
        for (int i=1;i<=n;i++) 
         for (int j=0;j<=m;j++)
          for (int k=0;k<M;k++) {
            dp[i][j][k]=dp[i-1][j][k];
            int t=k- (p[i]-d[i]);
            if (t<0||t>=M) continue;
            if (j<1) continue;
            check_max (dp[i][j][k],dp[i-1][j-1][t]+p[i]+d[i]);
          }
        int v=0;
        while (dp[n][m][st-v]<0&&dp[n][m][st+v]<0) v++;
        if (dp[n][m][st-v]>dp[n][m][st+v]) v=st-v;
        else v=st+v;
        int psum=0,dsum=0;
        int i=n,j=m;
        while (j) {
            if (dp[i][j][v]==dp[i-1][j][v]) i--;
            else {
                psum+=p[i];
                dsum+=d[i];
                ans.push_back (i);
                v-= (p[i]-d[i]);
                i--; j--;
            }
        }
        printf ("Best jury has value %d for prosecution and value %d for defence:
    ",psum,dsum);
        sort (ans.begin (),ans.end ());
        for (auto it:ans) printf ("%d ",it);
        printf ("
    
    ");
    }
    
    int main () {
        int kcase=0;
        while (scanf ("%d%d",&n,&m)) {
            if (n==0&&m==0) break;
            for (int i=1;i<=n;i++) scanf ("%d%d",&p[i],&d[i]);
            printf ("Jury #%d
    ",++kcase);
            f ();
        }
        return 0;
    }
    
  • 相关阅读:
    <q>标签,短文本引用
    使用<span>标签为文字设置单独样式
    html速查表
    禁用第三方键盘
    画虚线的方法 (记录)
    渐变色以及隐藏输入框光标
    iOS9 网络适配
    iOS 截屏/将图片存储到相册或沙盒目录下
    从任意字符串中获取所有的数字
    IOS开发
  • 原文地址:https://www.cnblogs.com/hhlya/p/14022092.html
Copyright © 2011-2022 走看看