poj3211 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
这道题是分组背包的变形,先用map把每个字符串相应每个数字,然后把同样的字符串归为一类,求出最小的时间,然后累加即可了,这里每一组的最短时间能够用01背包。由于是两人同一时候做,能够知道两人的工作量相差最小时。结束这样的颜色的衣服所用的时间最少。所以能够设背包容量为sum[i]/2,然后求这个容量下的最打容量。然后这组所加的时间就是sum[i]-dp[sum[i]/2].这里还要注意一点,初始化的时候不能初始化为-1,然后将dp[0]=0,由于不一定要恰好,细致理解一下:)
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int a[15][106],num[106],sum[106],dp[100500];
int main()
{
int n,m,i,j,t,c,ans,liang,k,num1;
char s[20];
map<string,int>hash;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0 && m==0)break;
memset(a,0,sizeof(a));
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(s,0,sizeof(s));
hash.clear();
num1=0;
for(i=1;i<=n;i++){
scanf("%s",s);
if(hash[s]==0){
num1++;hash[s]=num1;
}
}
for(i=1;i<=m;i++){
scanf("%d%s",&c,s);
t=hash[s];
a[t][++num[t]]=c;
sum[t]+=c;
}
ans=0;
for(i=1;i<=num1;i++){
if(num[i]==0)continue;
else{
//memset(dp,-1,sizeof(dp));
memset(dp,0,sizeof(dp));
liang=sum[i]/2;
for(k=1;k<=num[i];k++){
for(j=liang;j>=a[i][k];j--){
//if(dp[j-a[i][k]]!=-1)
dp[j]=max(dp[j],dp[j-a[i][k]]+a[i][k]);
}
}
ans=ans+sum[i]-dp[liang];
}
}
printf("%d\n",ans);
}
return 0;
}
poj3211 Washing Clothes的更多相关文章
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- POJ 3211 Washing Clothes(01背包)
POJ 3211 Washing Clothes(01背包) http://poj.org/problem?id=3211 题意: 有m (1~10)种不同颜色的衣服总共n (1~100)件.Dear ...
- POJ 3211 Washing Clothes 背包题解
本题是背包问题,可是须要转化成背包的. 由于是两个人洗衣服,那么就是说一个人仅仅须要洗一半就能够了,由于不能两个人同一时候洗一件衣服,所以就成了01背包问题了. 思路: 1 计算洗完同一颜色的衣服须要 ...
- [POJ 3211] Washing Clothes (动态规划)
题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...
- POJ 3211 Washing Clothes【01背包】
题意:给出n种颜色,m件衣服,再分别给出m件衣服的颜色,和洗所需要的时间,dearboy和他的妹子一起洗衣服,且同种颜色的衣服不能同时洗,也不能两个人同时洗一件衣服,问洗完这m件衣服至少需要的时间 先 ...
- POJ 3211 (分组01背包) Washing Clothes
题意: 小明有一个贤妻良母型的女朋友,他们两个一起洗衣服. 有M种颜色的N件衣服,要求洗完一种颜色的衣服才能洗另外一种颜色. 两人可以同时洗,一件衣服只能被一个人洗. 给出洗每件衣服所用的时间,求两个 ...
- poj 3211 Washing Clothes
// 题意 :夫妻两洗衣服,衣服有m种颜色,每种颜色又有若干件,每件衣服洗完需要特定的时间,要求每种颜色放在一起洗,洗完才能洗其他衣服.最后问洗完需要的最少时间// 将衣服按颜色分类 然后求出每种颜色 ...
- poj 3211 Washing Clothes(背包)
很不错的01背包!!! 不过有点疑问!!!(注释) #include <algorithm> #include<stdio.h> #include<string.h> ...
- POJ Washing Clothes 洗衣服 (01背包,微变型)
题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...
随机推荐
- 【Luogu】P2389电脑班的裁员(DP)
题目链接 sbt交了三遍才过是我的耻辱…… 就是设f[i][j]搞个三重循环DP一下,以上. #include<cstdio> #include<cstdlib> #inclu ...
- (转)Java字符串整形(例:0001)
原文地址:https://blog.csdn.net/xuexiiphone/article/details/51372692 caseID = preFix + String.format(&quo ...
- java面试题之什么是多线程上下文切换
多线程会共同使用一组计算机上的CPU,而线程数大于给程序分配的CPU数量时,为了让各个线程都有执行的机会,就需要轮流使用CPU.不同的线程切换使用CPU发生的数据切换等就是上下文切换
- jacoco功能测试覆盖率统计
1.在java程序的启动脚本(或者tomcat)中加入javaagent参数-javaagent:/home/apps/jacocoagent.jar=destfile=/home/apps/jaco ...
- d3 数据绑定
绑定过程 选择元素,绑定数据,追加元素 <!DOCTYPE html> <html> <head> <title>testD3-.html</ti ...
- net1:DateTime,Application与Session,
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- CSS3 动画卡顿性能优化解决方案--摘抄
最近在开发小程序,与vue类似,它们都有生命周期这回事. onLoad 监听页面加载 onReady 监听页面初次渲染完成 onShow 监听页面显示 到底是什么意思? 所以这又触碰到了我的知识盲区, ...
- touch上滑加载
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CodeForces 424D: ...(二分)
题意:给出一个n*m的矩阵,内有一些数字.当你从一个方格走到另一个方格时,按这两个方格数字的大小,有(升,平,降)三种费用.你需要在矩阵中找到边长大于2的一个矩形,使得按这个矩形顺时针行走一圈的费用, ...
- 2017-10-29-afternoon-清北模拟赛
T1 洗澡 贪心:将未匹配的右括号花费1变为左括号,最有多余的左括号有一半变成右括号 #include <cstring> #include <cstdio> ); int n ...