首页 > 其他分享 >杭电1171

杭电1171

时间:2023-02-06 21:04:56浏览次数:41  
标签:1171 int sum value 杭电 c2 c1 mount


Big Event in HDU

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

题意:
题意:有n样物品,每样物品价值是v,件数是m。尽量把这些物品分成两堆使得两边总价值最接近。输出分得的两堆各自的价值。
利用母函数法来解决,因为分成两堆,而两堆中较小的一堆最大为所有物品总价值量的一半,所以母函数的组合数上下就可以设置成总价值量的一半。求出所有的组合后,可以利用贪心的思想来得到答案,因为要求两堆之差尽可能小,所以就可以从总价值量的一半开始向小的方向找,找到最大的价值量,则另一堆的价值量就是总价值量-此堆的价值量。因为组合数可能较大,这里不记录组合种数,而是用一个标记来表示该数能否组合出即可。

不用算法1:

# include <iostream>
# include <algorithm>
using namespace std;

bool compare(int a,int b){

return a<b;
/*
< 升序 默认
> 降序
*/
}
int a[10000];
int main(){

int n,v,m,i,j,k;
int sum;

while(cin>>n,n>=0){

sum = 0;
int cnt = 0;
while(n--){
cin>>v>>m;
sum+=v*m;
while(m--){//获取m个v
a[cnt++] = v;
}
}

sort(a,a+cnt,compare);

int helf = sum/2;//取中间的数
int num = 0;
for(i=cnt-1;i>=0;i--){
if(num+a[i]>helf){
continue;
}
num+=a[i];//num<=helf
}
cout<<sum-num<<" "<<num<<endl;
}
return 0;
}

解法2:

# include <iostream>
# include <cstring>
# include <cstdio>
using namespace std;

# define MAX 250009
int c1[MAX],c2[MAX];


int main(){

int n,i,j,k;
int value[66],mount[66];
int sum = 0;
while(cin>>n,n>=0){
sum = 0;

memset(value,0,sizeof(value));
memset(mount,0,sizeof(mount));

for(i=0;i<n;i++){
cin>>value[i]>>mount[i];
sum+=value[i]*mount[i];
}

memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));

//对第一项进行初始化
for(i=0;i<=mount[0]*value[0];i+=value[0]){
c1[i] = 1;
}
int len = mount[0]*value[0];
for(i=1;i<n;i++){

for(j=0;j<=len;j++){//对mount[0]*value[0]的理解
for(k=0;k<=mount[i]*value[i];k+=value[i]){
c2[j+k] += c1[j];
}
}

len += mount[i]*value[i];
for(k=0;k<=len;k++){
c1[k] = c2[k];
c2[k] = 0;
}
}

for(i=sum/2;i>=0;i--){
if(c1[i]!=0){
printf("%d %d\n",sum-i,i);
break;
}
}

}


return 0;
}


标签:1171,int,sum,value,杭电,c2,c1,mount
From: https://blog.51cto.com/u_15955675/6040500

相关文章

  • 杭电1339
    ASimpleTaskProblemDescriptionGivenapositiveintegernandtheoddintegeroandthenonnegativeintegerpsuchthatn=o2^p.ExampleForn=24,o=3and......
  • 杭电1335-任意进制的转换
    BasicallySpeaking​​http://acm.hdu.edu.cn/showproblem.php?pid=1335​​ProblemDescriptionTheReallyNeatoCalculatorCompany,Inc.hasrecentlyhiredyourt......
  • 杭电1407--暴力与优化
    测试你是否和LTC水平一样高ProblemDescription大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上!你的任务是:计算方程x^2+y^2+z^2......
  • 杭电1114--完全背包
    Piggy-BankProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.Themainincomeforthisacti......
  • 杭电1266--数的倒叙
    ReverseNumberProblemDescriptionWelcometo2006’4computercollegeprogrammingcontest!Specially,Igivemybestregardstoallfreshmen!Youarethefuture......
  • 杭电1282-回文
    回文数猜想ProblemDescription一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数。任取一个正整数,如果不是回文数,将该数与他的......
  • 杭电2602---01背包
    骨收集器​​http://acm.hdu.edu.cn/showproblem.php?pid=2602​​问题描述许多年前,在泰迪的家乡有一个叫“拾骨者”的人。这个人喜欢收集不同的骨头,比如狗,牛,他还去了坟......
  • 杭电1248-背包
    院大学生程序设计竞赛(新生为主)寒冰王座TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):13570AcceptedSubm......
  • 杭电1201
    18岁生日ProblemDescriptionGardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都......
  • 杭电1202--学分
    ThecalculationofGPATimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):24013AcceptedSubmission(s):5610P......