HDU 1171 Big Event in HDU

news/2024/7/8 3:29:59 标签: HDU 1171

题目:HDU-1085

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1085

题目:

Big Event in HDU

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31958    Accepted Submission(s): 11183


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,怎么样分成价值最接近的两堆,每一堆价值多少。

额,这个题我还是用了母函数做的,这个构造的多项式以价值为指数,和其他的一样,只不过在得出结果以后我先筛选出非0的结果,排序以后再一个个比较哪个最接近,数据比较小所以也还好,没超时。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<cstdio>
using namespace std;
const int maxn= 250005;
int a[maxn],b[maxn];
int n;
int v,m,t;
int counts;
int maxNum,ans1,ans2;
int main(){
	while(cin>>n){
		if(n<0) break;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		cin>>v>>m;
		for(int i=0;i<=m;i++)
			a[i*v]=1;
		t=v*m;
		for(int i=1;i<n;i++){
			cin>>v>>m;
			t+=v*m;
			for(int j=0;j<=t;j++)
				for(int k=0;k<=v*m;k+=v)
					b[j+k]+=a[j];
			for(int j=0;j<=t;j++){
				a[j]=b[j];
				b[j]=0;
			}
		} //以上为构建母函数得出结果
		counts=0;
		for(int i=1;i<=t;i++){                        //筛选非0解
			if(a[i]!=0)
				b[counts++]=i;
		}
		sort(b,b+counts);                           //排序,其实不排也没什么
		maxNum=maxn;
		for(int i=0;i<counts;i++){                  //记住保证大的一堆在前面
			if(t-b[i]>=b[i]){
				if(t-b[i]-b[i]<maxNum){
					maxNum=t-b[i]-b[i];
					ans1=t-b[i];
					ans2=b[i];
				}
			}
			else{
				if(2*b[i]-t<maxNum){
					maxNum=2*b[i]-t;
					ans1=b[i];
					ans2=t-b[i];
				}
			}
		}
		cout<<ans1<<" "<<ans2<<endl;
	}
	return 0;
}

good good study, day day up!


http://www.niftyadmin.cn/n/1306268.html

相关文章

WebService之CXF注解之二(Service接口)

ITeacherService.java&#xff1a; /*** Title:ITeacherService.java* Package:com.you.service* Description:教师Service接口* author:Youhaidong(游海东)* date:2014-5-5 下午11:06:24* version V1.0*/ package com.you.service;import javax.jws.WebMethod; import javax.j…

HDU 1709 The Balance(母函数)

题目&#xff1a;HDU-1085 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1085 题目&#xff1a; The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7089 Accepted Submission(s…

httpwatch使用_使用JavaScript的HTTPWatch自动化

httpwatch使用背景(Background) HTTPWatch (automation)HTTPWatch (自动化)...with PHP (and again and again, and response) ...用PHP (一次又一次&#xff0c;和response )JavaScript shell scripting JavaScript Shell脚本I gave this short presentation at the recent Ya…

WebService之CXF注解之三(Service接口实现类)

ITeacherServiceImpl.java&#xff1a; /*** Title:ITeacherServiceImpl.java* Package:com.you.service.impl* Description:* author:Youhaidong(游海东)* date:2014-5-5 下午11:08:39* version V1.0*/ package com.you.service.impl;import com.you.model.Teacher; import co…

WebService之CXF注解之四(测试类)

TeacherTest.java&#xff1a; /*** Title:TeacherTest.java* Package:com.test.service* Description:* author:Youhaidong(游海东)* date:2014-5-5 下午11:14:09* version V1.0*/ package com.test.service;import org.apache.cxf.interceptor.LoggingInInterceptor; import …

div 自定义画布_通过画布自定义动画光标

div 自定义画布Warning: dont do this. Stop it! Just. Dont. 警告&#xff1a;请勿这样做。 停下来&#xff01; 只是。 别。 So theres this hack by Ben Foxall that shows how you can escape the browser window and draw outside the page. I had to try it myself. So …

U盘重装windows 10 系统教程

其实重装系统是一件很简单的事情&#xff0c;无论是不是计算机专业的学生或者从事计算机有关行业的人&#xff0c;只要是经常操作电脑的人&#xff0c;都可以很快的独立完成。当然&#xff0c;重装系统也有很多种方法&#xff0c;这里介绍的是使用U盘启动项来进行系统重装。操作…

mac上制作9.png_在Mac上安装一堆PNG工具

mac上制作9.pngThis is one of those note-to-self type of posts. Just went through the exercise of installing a number of PNG tools on the Mac and here are some notes. The instructions below should probably work on any unix box. 这是这些自我说明类型的帖子之…