zoukankan      html  css  js  c++  java
  • POJ 1014 Dividing

    套模板+填参数=AC

    POJ还是用C++提交靠谱
    Dividing
    Time Limit: 1000MSMemory Limit: 10000K
    Total Submissions: 51256Accepted: 12981

    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.

    Source


     
    #include <iostream>
    #include <cstring>

    using namespace std;

    int v;
    int dp[333333];//RE了就加个3
    void zpack(int* a,int c,int w)
    {
        for(int i=v;i>=c;i--)
            a=max(a,a[i-c]+w);
    }

    void cpack(int* a,int c,int w)
    {
        for(int i=c;i<=v;i++)
            a=max(a,a[i-c]+w);
    }

    void multipack(int* a,int c,int w,int m)
    {
        if(c*m>=v)
        {
            cpack(a,c,w);
            return ;
        }

        int k=1;
        while(k<m)
        {
            zpack(a,c*k,w*k);
            m-=k;
            k*=2;
        }

        zpack(a,c*m,w*m);
    }

    int main()
    {
        int cur=0;
    while(true)
    {
        int a[8];
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        int sum=0;
        for(int i=1;i<=6;i++)
        {
            cin>>a;
            sum+=a*i;
        }
        v=sum/2;
        if(sum==0) break;

        for(int i=1;i<=6;i++)
            multipack(dp,i,i,a);

        cout<<"Collection #"<<++cur<<":\n";
        if(sum-dp[v]==dp[v])
        cout<<"Can be divided."<<endl<<endl;
        else
        cout<<"Can't be divided."<<endl<<endl;
    }

        return 0;
    }

  • 相关阅读:
    java io 学习笔记(一)
    Centos中查看系统信息的常用命令
    arcgis影像批量裁剪代码
    VS2010中VC++目录和C/C++之间的区别。VC++ Directories和C/C++的区别。
    VS中为什么不同的项目类型属性查看和设置的界面不一样
    C++函数中返回引用和返回值的区别
    java中HashMap的keySet()和values()
    repoquery详解——linux查看包依赖关系的神器
    linux学习笔记
    log4j的AppenderLayout格式符
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351054.html
Copyright © 2011-2022 走看看