• poj1014解题报告


    Dividing
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 38124 Accepted: 9377

    Description

    Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble 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 marbles.

    Input

    Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000. 
    The last line of the input file will be "0 0 0 0 0 0"; do not process this line.

    Output

    For each collection, output "Collection #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.

    Sample Input

    1 0 1 2 0 0 
    1 0 0 0 1 1 
    0 0 0 0 0 0 

    Sample Output

    Collection #1:
    Can't be divided.
    
    Collection #2:
    Can be divided.
    解题思路:
      此题可以用动态规划很方便的实现,注意要剪掉不应该的尝试
    第一种情况: 对于从1~i中组合出m而言,如果1~i能组合的最大值<m,显然不用尝试;
    第二种情况:对于从1~i中组合出m而言,如果从1~k(k>i)中不能组合出m,则从1~i中显然也不能。
    当然还可能经过数学分析之后,有更好的剪枝效率。
    以下是代码:
    #include <iostream>
    #include
    <fstream>
    #define NUM 6
    using namespace std;
    bool f(int k,int current,int *number,int currentSum);
    //从1~k种面值中每种选取如干张组合成current,currentsum为1~k中各种面值*张数的和)
    short map[NUM+1][420001];//map[i][j]=1时表明从1~i种面值中组合出价值j是不可能的,所以从1~k(k<i)中组合出价值j肯定也不可能
    int main()
    {

    ifstream cin(
    "in.txt");
    ofstream cout(
    "out.txt");
    int number[NUM];
    int j=0;
    int group=0;
    while (1)
    {
    int sum=0;
    bool mark=false;
    for (j=0;j<NUM;j++)
    {
    cin
    >>number[j];
    if (number[j]!=0)
    {
    mark
    =true;
    }
    sum
    +=(j+1)*number[j];
    }
    if (!mark)
    {
    break;
    }
    group
    ++;
    if (sum%2!=0)
    {
    cout
    <<"Collection #"<<group<<":\n";
    cout
    <<"Can't be divided.\n";
    }
    else
    {
    // bool map[NUM][420001];
    memset(map,0,NUM*420001*sizeof(short));
    bool res=f(NUM,sum/2,number,sum);
    if (res)
    {
    cout
    <<"Collection #"<<group<<":\n";
    cout
    <<"Can be divided.\n";

    }
    else
    {
    cout
    <<"Collection #"<<group<<":\n";
    cout
    <<"Can't be divided.\n";
    }
    }
    cout
    <<"\n";
    }
    return 0;
    }

    bool f(int k,int current,int *number,int currentSum)
    {
    if (k>=0&&current==0) return true;
    else if(k==0&&current>0) return false;
    else
    {
    for (int t=0;t<=number[k-1];t++)
    {
    if (current-k*t>=0&&(currentSum-number[k-1]*k)>=(current-k*t))
    {
    int isearch=true;
    for(int j=k;j<=NUM;j++)
    {
    if (map[j][current-k*t])
    {
    isearch
    =false;
    break;
    }
    }
    if (isearch)
    {
    if (f(k-1,current-k*t,number,currentSum-number[k-1]*k)) return true;
    else map[k-1][current-k*t]=1; //表示未找到
    }

    }

    }
    map[k][current]
    =1; //表示未找到
    }
    return false;
    }


  • 相关阅读:
    【java Itext Pdf】itext pdf隔行换色 itext5添加表格背景颜色
    linux常用安装
    linux 定时执行shell脚本
    Linux crontab定时任务案例
    Oracle 常用的几个命令
    RS报表中根据变量比较大小来判断颜色
    如何利用TYPE 实现列转行
    Linux crontab概念
    关于下拉框联动选择的做法
    Oracle 启动常见的疑难
  • 原文地址:https://www.cnblogs.com/easyFancy/p/2073116.html
Copyright © 2020-2023  润新知