zoukankan      html  css  js  c++  java
  • POJ 3211 Washing Clothes

    Description

    Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes are in varieties of colors but each piece of them can be seen as of only one color. In order to prevent the clothes from getting dyed in mixed colors, Dearboy and his girlfriend have to finish washing all clothes of one color before going on to those of another color.

    From experience Dearboy knows how long each piece of clothes takes one person to wash. Each piece will be washed by either Dearboy or his girlfriend but not both of them. The couple can wash two pieces simultaneously. What is the shortest possible time they need to finish the job?

    Input

    The input contains several test cases. Each test case begins with a line of two positive integers M and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line contains M strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then follow N lines describing the clothes. Each of these lines contains the time to wash some piece of the clothes (less than 1,000) and its color. Two zeroes follow the last test case.

    Output

    For each test case output on a separate line the time the couple needs for washing.

    Sample Input

    3 4
    red blue yellow
    2 red
    3 blue
    4 blue
    6 red
    0 0

    Sample Output

    10

    Source

    额,01背包,開始以为贪心,我还是太年轻了不说了。策略例如以下:
    统计每种颜色衣服须要的总时间sum,然后sum/2进行01背包,看可以装下的最大时间
    详细见挫码=。=
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    using namespace std;
    int num[15][110];//统计每种颜色衣服各个数量所需时间
    int nu[15];//统计每种颜色衣服的数量
    char str[15][15];//读入字符
    int dp[10000];//背包,開始开下了,wa了=。=
    int n,m;
    int main()
    {
        int t;
        char s[110];
        while(~scanf("%d%d",&n,&m)&&(n+m))
        {
            getchar();
            for(int i=0;i<n;i++)
            {
                scanf("%s",str[i]);
                nu[i]=0;
            }
            for(int i=0;i<m;i++)
            {
                scanf("%d%s",&t,s);
                for(int j=0;j<n;j++)
                {
                    if(!strcmp(str[j],s))
                    {
                        nu[j]++;
                        num[j][nu[j]-1]=t;
                    }
                }
            }
            int ans=0;
            for(int i=0;i<n;i++)
            {
                if(nu[i])
                {
                    int sum=0;
                    for(int j=0;j<nu[i];j++)
                        sum+=num[i][j];
                    memset(dp,0,sizeof(dp));//01背包枚举每种物品
                    for(int j=0;j<nu[i];j++)
                    {
                        for(int k=sum/2;k>=num[i][j];k--)
                            dp[k]=max(dp[k],dp[k-num[i][j]]+num[i][j]);
                    }
                    ans+=max(dp[sum/2],sum-dp[sum/2]);
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

  • 相关阅读:
    Python 中的 __str__ 与 __repr__ 到底有什么差别
    02 LeetCode---链表相加
    01 LeetCode---两数之和
    python 数据结构一 之 线性表
    来自C++的"Const式"傲娇
    string 与 char 字符串区别-1
    超级有爱的并查集入门
    多项式求解
    竞码编程-蓝桥杯模拟赛4-D题
    树的直径-蓝桥杯大臣的旅费
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4291917.html
Copyright © 2011-2022 走看看