• poj 3211 Washing Clothes


     http://poj.org/problem?id=3211

    神奇01背包。

    只是有多组0-1背包,即每种颜色进行一次背包选择。
    > 选择的方式是先截取每种颜色总时间的一半,也就是说,至少要消耗一半的时间。
    > 然后用做 f[一半的时间] 数组最大值,背包之后,用总的时间减去得到的f[一半的时间]的最大值的差值,是消耗的最少时间。

    数组一定要开得很大。

    Washing Clothes
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 8478   Accepted: 2636

    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
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    int v[200][200],num[100010];
    int dp[100010],sum[100010];
    char str[20][20],str1[20];
    using namespace std;
    int main()
    {
        int m,n,i,r,j,q,t;
        int k,ans;
        while(~scanf("%d%d",&m,&n))
         {
            if(n==0||m==0)
               break;
           for(i=0;i<m;i++)
              scanf("%s",str[i]);
              memset(num,0,sizeof(num));
            for(i=0;i<n;i++)
            {
                scanf("%d",&t);
                scanf("%s",str1);
               for(j=0;j<m;j++)
               {
                  if(strcmp(str1,str[j])==0)
                      {
                          v[j][num[j]]=t;
                           num[j]++;
                           break;
                      }
                }
            }
          for(i=0;i<m;i++)
          {
              sum[i]=0;
              for(j=0;j<num[i];j++)
             {
                 sum[i]+=v[i][j];
             }
          }
             ans=0;
         for(i=0;i<m;i++)
          {
              memset(dp,0,sizeof(dp));
              for(j=0;j<num[i];j++)
               for(k=sum[i]/2;k>=v[i][j];k--)
                  {
                   dp[k]=max(dp[k],dp[k-v[i][j]]+v[i][j]);//01背包。
                  }
              ans+=sum[i]-dp[sum[i]/2];
          }
          printf("%d
    ",ans);
         }
        return 0;
    }
    
  • 相关阅读:
    Python基础学习参考(四):条件与循环
    Python基础学习参考(三):内置函数
    Python基础学习参考(二):基本语法
    2011的最后一篇博文 写给我自己也写给你们
    前端开发面试题
    常用js操作:
    两个嵌套for循环执行顺序
    在寻找学习js的途中,又发现了好的东西!
    arcgis api for js初学
    解决为什么arcgis api for js的first map里不显示arcgis地图
  • 原文地址:https://www.cnblogs.com/cancangood/p/3606300.html
Copyright © 2020-2023  润新知