题目
| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Problems Submit Problem Online Status Prob.ID: | Register Update your info Authors ranklist | Current Contest Past Contests Scheduled Contests Award Contest |
|
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 10181 | Accepted: 3264 |
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
[Submit]
[Go Back] [Status]
[Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
题解
- 看到这个题的时候就觉得输入很坑,最后果然死在输入的初始化上。。。
- 一开始觉得有点像统筹规划问题,后来想到用01背包解决。
- 为了不浪费时间,最好是两个人同时洗完某种颜色的衣服,或者让两人的时间差最小。最理想的就是两个人都花洗某种衣服总时间的一半,所以背包的总容量就应该是洗某种衣服总时间的一半,如果达不到,就达到最接近的值,因此用01背包解决。洗某个颜色的衣服最终花掉的时间就是两个人中时间较长的那个人花的时间,而这个值一定是sum_time-f[sum_time/2](因为f[sum_time/2]<=sum_time/2)
- 另外要注意的是,不能用首字母来区分不同颜色!!!
代码
// by spli
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
int m,n;
int col;
map<string,int>mch;
int v[15][110],sum[15];
int f[500*1010];
int main(){
scanf("%d%d",&m,&n);
while(n&&m){
col=0;//memset(mch,0,sizeof(mch));
mch.clear();
memset(v,0,sizeof(v));
memset(sum,0,sizeof(sum));
string s;
for(int i=1;i<=m;++i){
cin>>s;
mch[s]=++col;//cout<<s<<" ";
s.clear();
}
int x;
for(int i=1;i<=n;++i){
scanf("%d",&x);cin>>s;//cout<<s<<endl;
v[mch[s]][++v[mch[s]][0]]=x;
sum[mch[s]]+=x;
s.clear();
}
/*for(int i=1;i<=col;++i){
for(int j=1;j<=v[i][0];++j) cout<<v[i][j]<<" ";cout<<sum[i]<<" ";
cout<<endl;
}*/
int ans=0;
for(int t=1;t<=col;++t){
memset(f,0,sizeof(f));
for(int i=1;i<=v[t][0];++i)
for(int j=sum[t]/2;j>=v[t][i];--j)
f[j]=max(f[j],f[j-v[t][i]]+v[t][i]);
ans+=sum[t]-f[sum[t]/2];
}
printf("%d\n",ans);
scanf("%d%d",&m,&n);
}
return 0;
}