Divideing Jewels【dfs或DP】

Divideing Jewels Time Limit: 1 Sec Memory Limit: 128 MB Submit: 164 Solved: 22 [Submit][Status][Web Board] Description 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.

Input 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.

Output 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(Except the last case).

Sample Input1 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..

dp:

;const int MAXN = 100000;int p[MAXN],dp[MAXN];int vis[MAXN], tmp, ave;int main(){int cases = 1;while (1){memset(vis, 0, sizeof(vis));int ok = 0;int sum = 0;for (int i = 1; i <= 10; i++){scanf(“%d”,&p[i]);sum += i * p[i];if (p[i] != 0) ok = 1;}p[0] = 0;if (!ok) break;if (cases != 1) printf(“\n”);if (sum % 2 != 0){printf(“#%d:Can’t be divided.\n”, cases++);continue;}else{ave = sum / 2;memset(dp, -1, sizeof(dp));dp[0] = 0;for (int i = 0; i <= 10; i++){for (int j = 0; j <= ave; j++){if (dp[j] >= 0)dp[j] = p[i];else if (j < i || dp[j – i] <= 0){dp[j] = -1;}else{dp[j] = dp[j – i] – 1;}}}if (dp[ave]>=0)printf(“#%d:Can be divided.\n”, cases++);elseprintf(“#%d:Can’t be divided.\n”, cases++);}}return 0;}

dfs:

;const int MAXN = 1000000;int p[MAXN];int val[MAXN], tmp, ave;int dfs(int sum,int num){if (sum == 0) return 1;if (sum < 0 || (sum>0 && num == 0)) return 0;if (dfs(sum – val[num], num – 1)) return 1;dfs(sum,num-1);}int main(){int cases = 1;while (1){int sum = 0;int len = 1;for (int i = 1; i <= 10; i++){scanf(“%d”, &p[i]);sum += i * p[i];for (int j = 0; j < p[i]; j++)val[len++] = i;}if (sum==0) break;if (cases != 1) printf(“\n”);if (sum % 2 != 0){printf(“#%d:Can’t be divided.\n”, cases++);continue;}if (dfs(sum/2,len-1))printf(“#%d:Can be divided.\n”, cases++);elseprintf(“#%d:Can’t be divided.\n”, cases++);}return 0;}

,没有一种不通过蔑视、忍受和奋斗就可以征服的命运。

Divideing Jewels【dfs或DP】

相关文章:

你感兴趣的文章:

标签云: