zoukankan      html  css  js  c++  java
  • Washing Clothes 01背包的稍微变形 求每种衣服的中值

    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
    ***********************************************************************************************************************************************************
    01背包的变形
    ***********************************************************************************************************************************************************
     1 /*
     2 对同一种颜色的衣服尽可能的平均分成两组,
     3 每一组用01背包求最大值,每组的sum-最大值的和就是结果最优值
     4 */
     5 #include<iostream>
     6 #include<string>
     7 #include<cstring>
     8 #include<cstdio>
     9 #include<vector>
    10 using namespace std;
    11 vector<int>cost[1001];
    12 int dp[100001],n,m,cot,i,j,k;
    13 char  str[1001][10];
    14 int main()
    15 {
    16     while(scanf("%d%d",&n,&m)&&n&&m)
    17     {
    18         for(i=0;i<n;i++)
    19         {
    20          scanf("%s",str[i]);
    21          cost[i].clear();
    22         }
    23      while(m--)
    24       {
    25         char  ch[1001];
    26         scanf("%d%s",&cot,ch);
    27         for(i=0;i<n;i++)
    28           if(strcmp(str[i],ch)==0)break;
    29         if(i<n)
    30         {
    31             cost[i].push_back(cot);
    32         }
    33       }
    34      int ans=0;
    35       for(i=0;i<n;i++)
    36       {
    37         int siz=cost[i].size();
    38         if(siz>0)
    39         {
    40             memset(dp,0,sizeof(dp));
    41             int sum=0,mid,max1;
    42             for(j=0;j<siz;j++)
    43             sum+=cost[i][j];
    44             mid=sum/2;
    45             for(j=0;j<siz;j++)
    46             for(k=mid;k>=cost[i][j];k--)
    47             dp[k]=max(dp[k],dp[k-cost[i][j]]+cost[i][j]);
    48             max1=-1;
    49             for(j=0;j<=mid;j++)
    50              if(max1<dp[j])
    51                max1=dp[j];
    52             ans+=(sum-max1);
    53        }
    54        else
    55         continue;
    56       }
    57     printf("%d
    ",ans);
    58 
    59 }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    博客园美化-打赏代码
    苹果appID的获取方法
    简体、繁体相互转换
    iOS Socket编程(一)基本概念
    无线通信
    http与https通信
    iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
    iOS开发网络篇—GET请求和POST请求的说明与比较
    iOS开发网络篇—HTTP协议
    Verify the Developer App certificate for your account is trusted on your device.
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3364635.html
Copyright © 2011-2022 走看看