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

  • 相关阅读:
    MQTT入门1 -- mosquitto 安装
    利用wireshark抓取TCP的整个过程分析。
    ARM Linux驱动篇 学习温度传感器ds18b20的驱动编写过程
    移植ARM linux下远程连接工具dropbear
    飞凌2440开发板制作路由器
    基于视觉寻迹的寻路算法
    Linux I2C驱动架构
    Linux 设备树学习——基于i2c总线分析
    Linux SPI驱动学习——注册匹配
    从Linux内核LED驱动来理解字符设备驱动开发流程
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351054.html
Copyright © 2011-2022 走看看