http://poj.org/problem?id=3211
既然两个人可以同时洗同色的衣服,那么对每种颜色进行分析,转化为在sumtime/2的时间内能洗至多多久的衣服,则洗这种颜色的衣服至少用时sumtime-01pack(sumtime/2)
完整代码:
/*32ms,968KB*/
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
using namespace std;
int cost[15][105], num[15], dp[100005];
int _01pack(int V, int n, int kind)
{
memset(dp, 0, sizeof(dp));
int i, j;
for (i = 0; i < n; i++)
for (j = V; j >= cost[kind][i]; --j)
dp[j] = max(dp[j], dp[j - cost[kind][i]] + cost[kind][i]);
return dp[V];
}
int main()
{
int m, n, i, j, time, id, total_t, ans;
char color[15];
map<string, int> ma;
while (scanf("%d%d", &m, &n), m)
{
ma.clear();
memset(num, 0, sizeof(num));
memset(cost, 0, sizeof(cost));
for (i = 0; i < m; ++i)
{
scanf("%s", color);
ma[color] = i;
}
for (i = 0; i < n; ++i)
{
scanf("%d%s", &time, color);
id = ma[color];
cost[id][num[id]++] = time;
}
ans = 0;
for (i = 0; i < m; ++i)
{
total_t = 0;
for (j = 0; j < num[i]; ++j)
total_t += cost[i][j];
ans += total_t - _01pack(total_t >> 1, num[i], i);
}
printf("%d\n", ans);
}
return 0;
}