Poj1014 solution report

Source: Internet
Author: User

Poj 1014

Question: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1014

Obviously, many people in the discussion board want to take the model! However, it has been proved. The idea of modulus is wrong.

The following is the DP Solution. (From the discussion board)

Thought: This question is to find out whether there is a scheme to share marble by value. Because the marble cannot be damaged during allocation, there is an obvious pruning: when the total value of all marble is an odd number, it cannot be evenly divided. The problem is transformed into: can a person extract the marble whose total value is half of the original marble Stack from the original marble stack? The main algorithm of this question is dynamic planning. The array flag represents the State and sets the total value to sum. when flag [k] = true, it indicates that one person can obtain the value K, and the other person can obtain the value V-K of the marble distribution scheme. If flag [k] = false, the allocation scheme does not exist. our task is to calculate whether the flag [sum/2] is true or false. Obviously, the flag [0] = true solution exists, that is, a person does not know anything, another person took all the marble.

Set I (1 <6) as the value of the stone. Imagine if flag [k] = true, if you can add a marble with the value of I to K, then the flag [K + I] = true must be true. stone has two attributes. One is the value and the other is the quantity. Here array [I] represents the number of marble whose value is I. We divide the stages based on one of the attributes: value. That is, for (INT I = 1; I <= 6; I ++ ), flag [k] indicates whether a State exists (the State here refers to whether a new rock stack with a value of K can be split from the original rock stack ). In the initial phase, the value is I = 1, and the initial state is flag [0] = true. Max represents the maximum value of K that currently meets the flag [k] = true condition. For (Int J = max; j> = 0; j --) // starting from the current maximum flag, based on the theory that flag [J] = true is true, flag [J + I] = true is also true, add the stone in the stone [I] stage when the original state flag [J] = true already exists. Let's take a new State as an example: 3 0 1 2 0 0 flag []: sum/2 = 6i 0 1 2 3 4 5 6 flag []: 1 0 0 0 0 0 0 0 for I = 1 array [1] = 3 Because Flag [0] = true, so flag [1], flag [2], flag [3] becomes true: I 0 1 2 3 4 5 6 flag []: 1 1 1 1 0 0 0 for I = 2 array [2] = 0 no test for I = 3 array [3] = 1 Because Flag [0] flag [1], flag [2], flag [3] = true, so f Lag [3], flag [4], flag [5], and flag [6] All change to true: I 0 1 2 3 4 5 6 flag []: 1 1 1 1 1 1 1 1 etc. Our task is to determine whether flag [sum/2] is true.
Code:
#include <stdio.h>#include <stdlib.h>#include <string.h>bool flag[120001];int main(){int n =0;while(1){n++;int i,j,k,marble[7];int sum = 0,value =0;int ans=0;for(i=1;i<7;i++){scanf("%d",&marble[i]);sum += marble[i]*i;}if(sum == 0)break;value = sum/2;if((marble[1]==0 && marble[3]==0 && marble[5]==0 && value%2==1)|| sum%2==1){printf("Collection #%d:/nCan't be divided./n/n",n);continue;}memset(flag,0,sizeof(flag[0])*120001);flag[0]=true;int max = 0;for(i=1;i<7;i++){if(marble[i]>0){for(j=max;j>=0;j--){if(flag[j]){for(k=1;k<=marble[i];k++){if(j+k*i == value)ans = 1;else if(!flag[j+k*i]) {if(j+k*i > max)max = j+k*i;if(j+k*i > value) max = value;flag[j+k*i] = true;}}}}}}if(ans == 0)printf("Collection #%d:/nCan't be divided./n",n);elseprintf("Collection #%d:/nCan be divided./n",n);printf("/n");}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.