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

  • 相关阅读:
    Python--day68--ORM内容回顾
    Python--day67--include包含其他的url和反向解析URL
    Python--day67--Django的路由系统
    Python--day67--Jsonresponse响应介绍和路由系统的分组命名匹配方式(简单介绍)
    Python--day67--CBV和FBV、Request对象及上传文件示例
    Python--day66--Django模板语言关于静态文件路径的灵活写法
    GET和POST两种基本请求方法的区别
    ASP.NET中使用UpdatePanel实现局部异步刷新方法和攻略(转)
    GridView中实现DropDownList联动
    .NET string字符串的截取、移除、替换、插入
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351054.html
Copyright © 2011-2022 走看看