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继承关系的父子类中相同的成员变量
    MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除
    手动新建MVC控制器和视图,以及数据显示的问题
    创建简单的MVC项目
    复习i++和++j
    ViewBag的简单使用
    ValidationMessageFor验证
    Cookie的简单使用
    MVC3中 ViewBag、ViewData和TempData的使用和区别
    C#的GridView控件复习
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351054.html
Copyright © 2011-2022 走看看