Washing Clothes
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 9707   Accepted: 3114

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

-------------------------------------------------------
每种颜色相互独立,单独求解加起来即可(好像矩阵取数)
 
对于一种颜色,平均分配,假设时间和为sum,其中一个人的时间就是给一个容量为sum/2的背包装东西,v和w都是时间(01背包可行性,不一定装满,所以要保存w而不是1)
这样背包的价值就是一个人的用时,另一个人的用时是sum-背包价值,更新ans即可
[注意:和搭建双塔不同,本题一定要用所有“物品”,所以不用那么麻烦]
Bonus:每件衣服两人花的时间不同?   
f[i] 第一个人用时i时第二个人的最短用时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int N=,M=,T=1e5+;
int m,n,num=,cnt[M],ans=;
string s;
map<string,int> mp;
struct clo{
int t,col;
}a[N];
int f[T];
void dp(){
for(int c=;c<=m;c++){
int sum=cnt[c];
memset(f,,sizeof(f));
for(int i=;i<=n;i++) if(a[i].col==c)
for(int j=sum/;j>=a[i].t;j--)
f[j]=max(f[j],f[j-a[i].t]+a[i].t);
ans+=cnt[c]-f[sum/];
}
}
void init(){
mp.clear();
num=ans=;
memset(cnt,,sizeof(cnt));
}
int main(){
while(true){
scanf("%d%d",&m,&n);
if(m==&&n==) break;
init();
for(int i=;i<=m;i++){
cin>>s;
mp[s]=++num;
}
for(int i=;i<=n;i++){
scanf("%d",&a[i].t);
cin>>s;
a[i].col=mp[s];
cnt[a[i].col]+=a[i].t;
}
dp();
printf("%d\n",ans);
}
}
 
 

POJ3211 Washing Clothes[DP 分解 01背包可行性]的更多相关文章

  1. poj3211Washing Clothes(字符串处理+01背包) hdu1171Big Event in HDU(01背包)

    题目链接: id=3211">poj3211  hdu1171 这个题目比1711难处理的是字符串怎样处理,所以我们要想办法,自然而然就要想到用结构体存储.所以最后将全部的衣服分组,然 ...

  2. poj3211 Washing Clothes

    Description Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a ...

  3. HDU2191--多重背包(二进制分解+01背包)

    悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  4. [HDOJ5543]Pick The Sticks(DP,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...

  5. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  6. hdu1203 I NEED A OFFER!---概率DP(01背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材 ...

  7. poj1742(多重背包分解+01背包二进制优化)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  8. HDU 2955 Robberies(概率DP,01背包)题解

    题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...

  9. DP入门——01背包 & 完全背包

    01背包: 采药: https://www.luogu.org/problemnew/show/P1048 #include <iostream> #include <algorit ...

随机推荐

  1. 自定义Sublime Text图标详细教程

    改造后的Sublime 先看看我改造后的神器图标,接下来你就知道下面是干什么的了 :               之前无意间在刘哇勇的博客上看到这么一篇文章,很好奇就尝试着更改Sublime Text ...

  2. Weinre调试移动端页面

    Weinre是什么 如果我们做的是Cordova(phonegap)或其他hybird应用,当使用到原生功能时候(类似原生请求数据或页面切换时),没办法在PC chrome浏览器调试页面,一旦页面在手 ...

  3. kindeditor-在线编辑器

    写在前面的话: 今天是第一次写博客,很值得纪念,希望能够和大神们一起交流技术,一起进步...来自<一只有梦想的前端小白> 最近项目中需要实现图文混排的效果,所以研究了下在线编辑器-- ki ...

  4. Refusing to install webpack as a dependency of itself

    用npm安装webpack的时候报了这个错: Refusing to install webpack as a dependency of itself 翻译过来大概是:'拒绝安装webpack其本身 ...

  5. ArcGIS Server 标准版和高级版在Web3D的区别

  6. Autodesk hackathon 技术参考资料

    Autodesk 首届黑客马拉松(hackathon )将于6月14号在上海举行,在这次活动中,推荐您使用的技术有ReCap 360 photo照片建模技术和Autodesk 360 viewer通用 ...

  7. swift 中手势的使用

    swift 中手势的使用 /**点击手势*/ func tapGestureDemo() { //建立手势识别器 let gesture = UITapGestureRecognizer(target ...

  8. IOS整体项目层级构建

    在创建IOS项目时,若有一个比较明确的层级架构,将对于今后代码的维护或者功能的扩展很有帮助:本文将通过一个实例来展现我对于层级的一些观点:里面有一些零碎的知识点可能无法全部介绍,到时提供源代码进行下载 ...

  9. Android源码笔记——Camera系统架构

    Camera的架构与Android系统的整体架构保持一致,如下图所示,本文主要从以下四个方面对其进行说明. Framework:Camera.java Android Runtime:android_ ...

  10. 常见HTTP状态码

    常见HTTP状态码 200 OK 301 Moved Permanently 302 Found 304 Not Modified 307 Temporary Redirect 400 Bad Req ...