nyoj 546
来源:互联网 发布:数据上限下限设定 编辑:程序博客网 时间:2024/05/21 17:48
Divideing Jewels
- 描述
Mary and Rose own a collection of jewells. They want to split the collection among themselves so that both receive an equal share of the jewels. This would be easy if all the jewels had the same value, because then they could just split the collection in half. But unfortunately, some of the jewels are larger, or more beautiful than others. So, Mary and Rose start by assigning a value, a natural number between one and ten, to each jewel. Now they want to divide the jewels so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the jewels in this way (even if the total value of all jewels is even). For example, if there are one jewel 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 jewels.
- 输入
- Each line in the input file describes one collection of jewels to be divided. The lines contain ten non-negative integers n1 , . . . , n10 , where ni is the number of jewels of value i. The maximum total number of jewells will be 10000.
The last line of the input file will be "0 0 0 0 0 0 0 0 0 0"; do not process this line. - 输出
- For each collection, output "#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. - 样例输入
1 0 1 2 0 0 0 0 2 01 0 0 0 1 1 0 0 0 00 0 0 0 0 0 0 0 0 0
- 样例输出
#1:Can't be divided.#2:Can be divided.
背包+二进制优化
#include "stdio.h"#include "string.h"int sum;int a[11];bool mark[100005];void CompletePack(int cost){ for(int i=0; i<=sum-cost; ++i) { if(mark[i]) mark[i+cost] = true; }}void ZeroOnePack(int cost){ for(int i=sum; i>=cost; i--) { if(mark[i-cost]) mark[i] = true; }}void dp(int cost,int k){ if(cost*k>=sum) CompletePack(cost); //完全背包 else //二进制优化 { for(int j=1; j<k; ) { ZeroOnePack(j*cost); k -= j; j*=2; } ZeroOnePack(k*cost); }}int main(){ int i; int Case=0; while(1) { Case++; sum = 0; for(i=1; i<=10; ++i) { scanf("%d",&a[i]); sum += i*a[i]; } if(sum==0) break; if(sum%2==1) { printf("#%d:Can't be divided.\n\n",Case); continue; } sum = sum>>1; memset(mark,false,sizeof(mark)); mark[0] = true; for(i=1; i<=10; ++i) dp(i,a[i]); if(mark[sum]) printf("#%d:Can be divided.\n\n",Case); else printf("#%d:Can't be divided.\n\n",Case); } return 0;}
- nyoj 546
- NYOJ 546 Divideing Jewels
- nyoj 546 Divideing Jewels
- NYOJ 546 Divideing Jewels
- nyoj 546 Divideing Jewels
- nyoj 546 Divideing Jewels
- NYOJ:546 Divideing Jewels
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- NYOJ
- Leetcode_recover-binary-search-tree
- 去了新博客
- VC文件的读写操作中的同步和异步
- 位域结构体
- 在Myeclipse中设置jsp页面为默认utf-8编码
- nyoj 546
- screenX clientX pageX概念
- 那些关于Java的小秘密
- MVC 路由机制和重修路由
- jQuery-瀑布流的取巧做法分享
- autolayout下的uiscrollview
- Linux内核开机打印信息
- RandomAccess接口介绍
- SuperMap(服务)- ArcGis(展现)兼容开发