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

  • 相关阅读:
    【前端进阶】VUE高性能组件引用
    「前端进阶」高性能渲染十万条数据(虚拟列表) (自己修改版本)
    页面缓存、离线存储技术localforage(案例篇)
    页面缓存、离线存储技术localforage(介绍篇)
    websocket快速搭建(node+websocket)
    一款程序员的杀手级应用:TabNine代码补全工具
    如何把es6的代码转成es5,ECMAScript 2015+代码转换神器——Babel
    如何使用echarts画一个简单k线图
    深入浅出理解 . 深拷贝 . 浅拷贝
    JS高级一看就懂什么是原型链
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4291917.html
Copyright © 2020-2023  润新知