TOJ 4119 Split Equally

编程入门 行业动态 更新时间:2024-10-10 17:31:26

<a href=https://www.elefans.com/category/jswz/34/1443638.html style=TOJ 4119 Split Equally"/>

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;
}

 

 

转载于:.html

更多推荐

TOJ 4119 Split Equally

本文发布于:2024-02-14 13:43:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1763758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TOJ   Equally   Split

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!