• 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;
    }

  • 相关阅读:
    51单片机寄存器组的设置(转)
    51单片机堆栈深入剖析(转)
    do{...}while(0)的妙用(转)
    优化C/C++代码的小技巧(转)
    Struts2返回json
    详略。。设计模式1——单例。。。。studying
    [深入理解Android卷一全文-第十章]深入理解MediaScanner
    《python源代码剖析》笔记 Python虚拟机框架
    jQuery Validation让验证变得如此easy(三)
    mysql高可用架构方案之中的一个(keepalived+主主双活)
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351054.html
Copyright © 2020-2023  润新知