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. bootstrapcss3触屏滑块轮播图

    插件描述:bootslider响应bootstrapcss3触屏滑块轮播图 小海已经好久没分享技术性文章了,这个基于bootstrap的触屏版轮播图绝对满足大家的胃口,并且支持移动端触摸滑动.功能上, ...

  2. TortoiseSVN使用教程

    一.下载安装文件: 图1.1 安装顺序:1.VisualSVN-Server-2.6.5.msi:2.TortoiseSVN-1.8.2.24708-win32-svn-1.8.3.msi:3.Lan ...

  3. Mac地址泛洪攻击的防御措施和具体配置

    Mac地址泛洪攻击指的是:利用交换机的mac地址学习机制,攻击者不断地刷新mac地址,填满交换机的mac地址表,以致崩溃,使交换机不得不使用广播发包,从而获取其他人的报文信息. mac地址泛洪攻击的防 ...

  4. 2013 Visual Studio Magazine读者选择奖界面框架类获奖情况

    2013 Visual Studio Magazine读者选择奖已经正式揭晓了!据了解,截至今年此奖项已经评选了21次,非常值得.NET开发人员信赖和参考.此次评选共有400多个产品角逐28个分类的奖 ...

  5. UITableViewController和延时执行、自定义控件

    1.在UITableViewController中,self.view就是self.tableView, 两个对象的指针地址是一样的 2.自定义类继承UITableViewCell, 重写父类方法 / ...

  6. UITableView

    知识点: 1)UITableView 2)UITableViewCell ====================================================== 一.UITabl ...

  7. Spring AOP 四大通知

    Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现  MethodBeforeAdvice 接口,重写 public  void  before(Method  metho ...

  8. C语言printf()输出格式大全

    1.转换说明符       %a(%A)     浮点数.十六进制数字和p-(P-)记数法(C99)       %c             字符       %d             有符号十 ...

  9. 如何使用SSL pinning来使你的iOS APP更加安全

    SSL pinning在构建一个高度安全的移动APP上扮演了一个十分重要的角色.然而如今好多用户在使用无线移动设备去访问无数不安全的无线网络. 这篇文章主要覆盖了SSL pinning 技术,来帮助我 ...

  10. 【原】iOS:一种直接修改frame的某个属性的方法

    在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法对frame的某个属性(x,y,width,height等)进行单独修改,比如: someView.f ...