zoukankan      html  css  js  c++  java
  • Big Event in HDU

    //转 背包做

    题意:求把总价值分为两个数,使这两个数接近相等,而且这两个数必须由所有设备中的其中几种设备价值构成,并先输出比较大的数,再输出另一个数。

    解题思路:DP算法,背包问题,求法是先求出总价值sum,再用dp[]求sum/2最多能放多少价值!即可以求出其中一个数了,另一个就是sum-dp[sum/2]了……
    状态:f[j]:表示软件学院取得容量为j时能获得的最大值,j<=sum/2; 状态转移:f[j]=max{f[j], f[j-v[i]]+v[i]}

    注意数组的大小(0<n<=50,0<v<=50,0<m<=100)

    Problem Description

    Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.

    The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).

    Input

    Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.

    A test case starting with a negative integer terminates input and this test case is not to be processed.

    Output

    For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.

    Sample Input

    2

    10 1

    20 1

    3

    10 1

    20 2

    30 1

    -1

    Sample Output

    20 10

    40 40

     1 #include<stdio.h>
     2 
     3 #include<string.h>
     4 
     5 
     6 
     7 int s[100005];
     8 
     9 int dp[222222];
    10 
    11 int max(int a,int b)
    12 
    13 {
    14 
    15 return a>b?a:b;
    16 
    17 }
    18 
    19 
    20 
    21 int main()
    22 
    23 {
    24 
    25 int n,v,m;
    26 
    27 while(~scanf("%d",&n)&&n>0)
    28 
    29 {
    30 
    31 int k=0;
    32 
    33 int sum=0;
    34 
    35 memset(s,0,sizeof(s));
    36 
    37 memset(dp,0,sizeof(dp));
    38 
    39 for(int i=0;i<n;i++)
    40 
    41 {
    42 
    43 scanf("%d %d",&v,&m);
    44 
    45 while(m--)
    46 
    47 {
    48 
    49 s[k++]=v;//将价值存入数组  
    50 
    51 sum+=v;
    52 
    53 }
    54 
    55 }
    56 
    57 for(int i=0;i<k;i++)
    58 
    59 for(int j=sum/2;j>=s[i];j--)
    60 
    61 {
    62 
    63 dp[j]=max(dp[j],dp[j-s[i]]+s[i]);
    64 
    65 }
    66 
    67 printf("%d %d
    ",sum-dp[sum/2],dp[sum/2]);
    68 
    69 }
    70 
    71 return 0;
    72 
    73 }
    View Code
  • 相关阅读:
    Spring Boot重定向的使用方法
    Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
    Jmeter性能测试之分布式(五)
    Jmeter性能测试之Monitor监控(四)
    Jmeter性能测试之关联(三)
    Jmeter性能测试之参数化(二)
    JVM 内存溢出(转载~)
    MySql workbeach 更改侧边栏大小
    Mybati example generatorConfig.xml 配置详解
    Rvm 进行gem安装时必须输入密码Your user account isn't allowed to install to the system RubyGems 解决方案
  • 原文地址:https://www.cnblogs.com/awsent/p/4266896.html
Copyright © 2011-2022 走看看