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?
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.
For each test case output on a separate line the time the couple needs for washing.
3 4 red blue yellow 2 red 3 blue 4 blue 6 red 0 0
10
做这道题的时候很迷。。。原先做出了多重部分和的题,然后发现这题其实差不多,只是非多重,于是我尝试优化过的一维数组的方法做,结果TLE,于是就想着尝试用多重背包定义来做,但是由于一开始有个细节打错了,导致后面提交上去是runtime error ,既然有问题,就得解决,所以我用了一组非常大的数据测试,m = 9,n = 99,然后每个颜色的值都赋予999,还在中间输出了sum的值,发现除开一开始,后面sum都是负的,而且明明只应该有9个sum,却输出了很多,因此我断定循环条件出错,果然在本该是m的地方我用了n,结果当然ac啦,一开始还以为会超时,等测评结束发现其实才400多ms,还好。。。。
贴下代码,简单的思路:
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define INF 1e8
using namespace std;
int m,n;
int ans;
char s[10][20],s2[20];
int cloth[10][1005];
int t[105],tt[105];
int dp[1000000];
int main(void)
{
int b;
while (scanf("%d %d",&m,&n)!=EOF&&(n||m))
{
ans = 0;
memset(cloth,0,sizeof(cloth));
for (int i = 0;i < m;i++)
scanf("%s",s[i]);
for (int j = 0;j < n;j++)
{
scanf("%d %s",&b,s2);
for (int k = 0;k < m;k++)
{
if(strcmp(s2,s[k])==0)
{
cloth[k][b]++;
}
}
}
for (int i = 0;i < m;i++)
{
int sum = 0;
int tlen = 1;
for (int j = 0;j < 1005;j++)
{
if(cloth[i][j]!=0){
t[tlen]=j;
tt[tlen++]=cloth[i][j];
sum+=j*cloth[i][j];
}
}
//cout<<sum<<endl;
if(sum==0)
continue;
int ssum,pans;
ssum = sum/2;
memset(dp,-1,sizeof(dp));
dp[0]=0;
for (int j = 1;j < tlen;j++)
{
for (long long int k = 0;k <= ssum;k++)
{
if(dp[k]>=0)
{
dp[k]=tt[j];
}
else if(k-t[j]<0||dp[k-t[j]]<0)
{
dp[k]=-1;
}
else
dp[k]=dp[k-t[j]]-1;
}
}
for (int k = ssum;k >= 0;k--)
{
if(dp[k]>=0)
{
pans=k;
break;
}
}
ans=ans+sum-pans;
}
cout<<ans<<endl;
}
return 0;
}
后来我将n改回m后,用原来的代码,发现原来的代码反而更省时间,才跑了几十ms。。。。。
所以,还是有必要贴一下这个代码
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define INF 1e8
using namespace std;
int m,n;
int ans;
char s[10][20],s2[20];
int cloth[10][1005];
int t[105];
int dp[100000];
int main(void)
{
int b;
while (scanf("%d %d",&m,&n)!=EOF&&(n||m))
{
ans = 0;
memset(cloth,0,sizeof(cloth));
for (int i = 0;i < m;i++)
scanf("%s",s[i]);
for (int j = 0;j < n;j++)
{
scanf("%d %s",&b,s2);
for (int k = 0;k < m;k++)
{
if(strcmp(s2,s[k])==0)
{
cloth[k][b]++;
}
}
}
for (int i = 0;i < m;i++)
{
int sum = 0;
int tlen = 1;
for (int j = 0;j < 1005;j++)
{
while (cloth[i][j]!=0)
{
cloth[i][j]--;
t[tlen++]=j;
sum+=j;
}
}
if(sum==0)
continue;
int ssum;
ssum = sum/2;
int pans=0;
memset(dp,0,sizeof(dp));
dp[0]=1;
for (int j = 1;j < tlen;j++)
{
for (int k = pans;k >= 0;k--)
{
if(dp[k+t[j]]==1)
continue;
if(dp[k]==1&&k+t[j]<=ssum)
{
dp[k+t[j]]=1;
pans=max(pans,k+t[j]);
}
}
//cout<<pans<<" ";
}
//cout<<endl;
pans = sum-pans;
ans+=pans;
}
cout<<ans<<endl;
}
return 0;
}