zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    C# 遍历类字段 及 字段值
    u8 采购到货单中的 业务类型 没有表字典,是系统预置 存入表也是文字: 普通采购 固定资产
    win7 X64 OLEDB读取EXCEL提示“未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序”
    关于 C# select 读取 Excel 数据 为空,或成科学计数法解决方案
    如何设置C#程序在Windows 7 Vista下以管理员权限运行
    C# OLE DB 的连接方式下 对 sql 参数的操作 需要改成 ? 号
    NHibernate (一) 五部曲
    做优秀产品经理
    Python Test in Hudson
    [转载]Openstack Essex安装文档资料
  • 原文地址:https://www.cnblogs.com/easyFancy/p/2073116.html
Copyright © 2011-2022 走看看