TOJ 4119 Split Equally

描述

Two companies cooperatively develop a project, but they don’t like working with one another. In order to keep the company together, they have decided that only one of them will work on each job. To keep things fair, the jobs need to be split so that both work the same amount. This is accomplished by estimating the time necessary for each job and assigning jobs based upon these  estimates. The purpose of your program is to determine whether the jobs can be equally split (the total estimated time necessary for both individuals sums to the same value).
 

输入

The input will consist of multiple test cases (one per line terminated by a line containing the string END). Each line will contain a space seperated list of integers which are the estimated times required for the jobs.

输出

For each case your output should be either YES or NO depending on whether they can be split equally.

样例输入

1 2 3 4 5 6
2 3 4 5 6 8
7 2 4 8 11 16
9 10 11 30
14 12 9 15
END

样例输出

NO
YES
YES
YES
NO

题目来源

TOJ

 

01背包问题,背包问题只会01了。%>_<%

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int n,w[50001]={0};
int best[2][50001],sum,c;

int main()
{
	char ch[52000];
	while(gets(ch),strcmp(ch,"END")!=0){
		//初始化 
		int n=1,sum=0;		
		char *p=strtok(ch," ");
		w[n]=atoi(p);
		sum+=w[n++];
		while(p=strtok(NULL," ")){
			w[n]=atoi(p);
			sum+=w[n++];
		}
		c=sum/2;
		n--;
		memset(best[0],0,sizeof(best[0])); 
		for(int i=1;i<=n;i++){
			for(int j=1;j<=c;j++){
				if(j>=w[i]){
					if(best[(i-1)%2][j-w[i]]+w[i]>=best[(i-1)%2][j]){
						best[i%2][j]=best[(i-1)%2][j-w[i]]+w[i];							
					}else {
						best[i%2][j]=best[(i-1)%2][j];		
					}					
				}else{
					best[i%2][j]=best[(i-1)%2][j];
				}
			}
		}
		if(best[n%2][c]==sum-best[n%2][c]) {
			puts("YES");
		}else{
			puts("NO");
		}
	}
	return 0;
}

 

 

你可能感兴趣的:(split)