衣服只能一种颜色一种颜色的洗,把每种颜色分开洗,总时间减去半时间内洗得最多的情况下一个洗得最少的就行。
这一题过得很爽,一次写完,运行发现一个减号习惯性地写成加号外提交就成了。只是写得比较绕一点,感觉。其实可以弄一个
函数在前面的,更有层次感觉,不过我就是在练习我的这种思维。
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
#include<stdlib.h>
#include<list>
#include<vector>
using namespace std;
int colornum,clothnum;
char color[20][20];
int cloth[20][200];
int cloths[20];
int dp[1000000];
int sum[20];
int main()
{
int i,j,k;
while (cin>>colornum>>clothnum,colornum||clothnum)
{
for (i=0;i<colornum;i++)
cin>>color[i];
char cur[20];
memset(sum,0,sizeof(sum));
memset(cloths,0,sizeof(cloths));
for (i=0;i<clothnum;i++)
{
cin>>k>>cur;
for (j=0;j<colornum;j++)
{
if (strcmp(cur,color[j])==0)
{
sum[j]+=k;
cloth[j][cloths[j]++]=k;
break;
}
}
}
int ans=0;
for (i=0;i<colornum;i++)
{
for (j=0;j<=sum[i]/2;j++)
{
dp[j]=0;
}
for (j=0;j<cloths[i];j++)
{
for (k=sum[i]/2;k>=cloth[i][j];k--)
{
dp[k]=max(dp[k],dp[k-cloth[i][j]]+cloth[i][j]);
}
}
ans+=sum[i]-dp[sum[i]/2];
}
cout<<ans<<endl;
}
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 6850 | Accepted: 1978 |
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 integersM and N (M < 10, N < 100), which are the numbers of colors and of clothes. The next line containsM strings which are not longer than 10 characters and do not contain spaces, which the names of the colors. Then followN 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