【二进制拆分多重背包】【HDU1059】【Dividing】

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18190    Accepted Submission(s): 5080


Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. 
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
 

Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000. 

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
 

Output
For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''. 

Output a blank line after each test case.
 

Sample Input
 
  
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
 

Sample Output
 
  
Collection #1: Can't be divided. Collection #2: Can be divided.
 

Source
 

一开始还觉得有点难写。。


后来AC才感觉自己编码能力已经很强了。。


主要是这个拆分函数就OK了


void solve()
{
	for(int i=1;i<=6;i++)
	{
		int temp=0;
		while(n[i]>0)
		{
			if(n[i]>=ER[temp])
				A[++tot]=i*ER[temp];
			else 
				A[++tot]=i*n[i];
			n[i]=n[i]-A[tot]/i;
			temp++;
		}
	}	
}


完整代码如下:

#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313   
#define MAX 2100000000
using namespace std;
void init()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
}
int n[10];
int F[100001];
int ER[100];
int K;
int A[600];
int tot=0; //总数 
void YCL()
{
	memset(F,0,sizeof(F));
	tot=0; 
	K=0;
	for(int i=1;i<=6;i++)
		{
			K+=i*n[i];
		}
	for(int i=1;i<=K/2;i++)
	F[i]=MAX;
	F[0]=0;
}
void ER1()
{
	ER[0]=1;
	for(int i=1;i<=15;i++)
	{
		ER[i]=ER[i-1]*2;
	}
}
void solve()
{
	for(int i=1;i<=6;i++)
	{
		int temp=0;
		while(n[i]>0)
		{
			if(n[i]>=ER[temp])
				A[++tot]=i*ER[temp];
			else 
				A[++tot]=i*n[i];
			n[i]=n[i]-A[tot]/i;
			temp++;
		}
	}	
}
int main()
{
//	init();
	int CASE=0;
	ER1();			//得到二的倍数 
	while(cin>>n[1]>>n[2]>>n[3]>>n[4]>>n[5]>>n[6]&&(n[1]||n[2]||n[3]||n[4]||n[5]||n[6]))
	{
		CASE++;
		YCL();			//计算K 
		solve();        //拆分开始 
		if(K%2==0)
		{
			for(int i=1;i<=tot;i++)
			 for(int j=K/2;j>=0;j--)
			   {
			   	if(j-A[i]>=0)
			   	F[j]=min(F[j],F[j-A[i]]);
			   }
			printf("Collection #%d:\n",CASE);
			if(F[K/2]!=MAX) 
		{
			printf("Can be divided.\n\n");
		}
		else 
		{
				printf("Can't be divided.\n\n");
		}
		}
		else
		{
			printf("Collection #%d:\n",CASE);
			printf("Can't be divided.\n\n");
		}
	}
	return 0;
}
  

恩 再补一道同类型的题目


HDU(3732)

Ahui Writes Word

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2214    Accepted Submission(s): 811


Problem Description
We all know that English is very important, so Ahui strive for this in order to learn more English words. To know that word has its value and complexity of writing (the length of each word does not exceed 10 by only lowercase letters), Ahui wrote the complexity of the total is less than or equal to C.
Question: the maximum value Ahui can get.
Note: input words will not be the same.
 

Input
The first line of each test case are two integer N , C, representing the number of Ahui’s words and the total complexity of written words. (1 ≤ N ≤ 100000, 1 ≤ C ≤ 10000)
Each of the next N line are a string and two integer, representing the word, the value(Vi ) and the complexity(Ci ). (0 ≤ Vi , Ci ≤ 10)
 

Output
Output the maximum value in a single line for each test case.
 

Sample Input
 
   
5 20 go 5 8 think 3 7 big 7 4 read 2 6 write 3 5
 

Sample Output
 
   
15
Hint
Input data is huge,please use “scanf(“%s”,s)”
 

#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <cstring>  
#include <ctime>  
#include <algorithm>  
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313   
using namespace std;
int N,C;
char buffer[30];
int map[20][22];
int ER[30];
int tot;
int v[3000];
int w[3000];
int F[10001];
void ER1()
{
	ER[0]=1;
	for(int i=1;i<=20;i++)
	{
		ER[i]=ER[i-1]*2;
	}
}
void input()
{
	int a,b;
	tot=0;
	memset(map,0,sizeof(map));
	memset(F,0,sizeof(F));
	for(int i=1;i<=N;i++)
	{
		scanf("%s %d %d",buffer,&a,&b);
		map[a][b]++;
	}
}
void init()
{
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
}
void solve()
{
	for(int i=1;i<=10;i++)
	 for(int j=0;j<=10;j++)
	 {
	 	int temp=0;
		while(map[i][j]>0)
	 	{
	 		if(map[i][j]>=ER[temp])
			{
				v[++tot]=ER[temp]*i;
				w[tot]=ER[temp]*j;
			}
			else
			{
				v[++tot]=map[i][j]*i;
				w[tot]=map[i][j]*j;
			}
			{
				map[i][j]=map[i][j]-v[tot]/i;
				temp++;
			}
		}
	 }
}
void dp()
{
	for(int i=1;i<=tot;i++)
	 for(int j=C;j>=0;j--)
	 {
	 	if(j-w[i]>=0)
	 	F[j]=max(F[j],F[j-w[i]]+v[i]);
	 }
}
int main()
{
//	init();
	ER1();
	while(cin>>N>>C)
	{
		input();
		solve();
		dp();
		printf("%d\n",F[C]);
	}
	return 0;
}
  


转载于:https://www.cnblogs.com/zy691357966/p/5480417.html

Eclipse二进制文件是指由Eclipse集成开发环境生成的可执行文件。在Linux下,Eclipse生成的二进制文件通常是没有扩展名的,如上述引用所示。在使用Eclipse编译和运行项目时,如果项目名包含扩展名(例如.hdu.c),则Eclipse可能无法正确识别该文件为二进制可执行文件。 这可能导致在运行时出现找不到二进制文件的错误。解决办法有两种:一是避免使用带有扩展名的项目名,另一种是手动创建一个运行配置,将命令写死以确保正确识别为二进制文件。 需要注意的是,上述讨论中的EclipseParser库是一个用于验证和读取Eclipse二进制文件内容的DLL库,它是通过按照二进制格式编写的。这个库可以验证和读取Eclipse二进制文件中的EGRID、INIT、UNRST等内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [linux下eclipse c++运行不了提示找不到二进制文件的解决方法](https://blog.csdn.net/bjrxyz/article/details/8974483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Eclipse油藏数值模拟软件的二进制文件格式解析](https://blog.csdn.net/slofslb/article/details/119176891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值