poj 3211 Washing Clothes(0/1背…

题意:有n(1~10)种不同颜色的衣服总共m (1~100)件,Dearboy和她的girlfriend两个人要一起洗完全部衣服,
两个人洗衣速度相同,并且已知每件衣服需要的时间<1000)。 两个人可以同时洗衣。为了预防色彩混合,必须一种颜色的衣服洗完之后,两个人才能开工洗下一种颜色的衣服,问两个人洗完所有的衣服需要的最短时间。

思路:每种颜色的衣服可以分开来考虑,算出每种颜色的衣服所需要的最短时间,最后加起来即可。
然后再来考虑单一一种颜色的衣服该怎么洗,考虑到可能一个人要多洗一会儿,一个人要少洗一会儿, 两个人所花的时间越接近一个人洗时总时间的一半,肯定是越好的方案,所以,用一个人洗时总时间的一半做容量,所有的衣服所花的时间做容量与价值(容量等于价值),转化成n个01背包, 用01背包求出最大价值就是花时间少的那个人所用的时间了, 然后用总时间减去这个花时间少的人的用时,就得到了用时多的人的用时了。

这回把代码写的太乱了,唉~ 就当到此一游吧,不注释了。。。

//208K      16MS
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define M 20
#define N 150
using namespace std;
struct data
{
      int time;
      char col[M];
}node[N];
int max (int a, int b)
{
      return a > b? a : b;
}
bool cmp (data a,data b)
{
      if (strcmp (a.col,b.col) >= 0)
              return true;
      return false;
}
int main ()
{
      int n,m,i,j,k,ans;
      int t[N],dp[10000];
      char str[N+10];
      while (scanf ("%d%d",&m,&n))
      {
              if (m == 0&&n == 0)
                      break;
              getchar ();
              gets (str);

              for (i = 0;i < n;i ++)
                      scanf ("%d %s",&node[i].time,node[i].col);
              sort (node,node + n,cmp);
              strcpy (node[n].col,"##");
              node[n++].time = 0;
              strcpy (str,node[0].col);
              k = 0;
              ans = 0;
              for (i = 0;i < n;i ++)
              {
                      if (strcmp (str,node[i].col) == 0)
                      {
                              t[k++] = node[i].time;
                      }
                      else
                      {
                              m = k;
                              int p,q;
                              int cost,sum = 0;
                              for (p = 0;p < m;p ++)
                                      sum += t[p];
                              cost = sum/2;
                              memset (dp,0,sizeof(dp));
                              for (p = 0;p < m;p ++)
                              {
                                      for (q = cost;q >= t[p];q --)
                                              dp[q] = max (dp[q],dp[q-t[p]]+t[p]);
                              }
                              k = 0;
                              ans += max (dp[cost],sum - dp[cost]);
                              strcpy (str,node[i].col);
                              -- i;
                      }
              }
              printf ("%d\n",ans);
      }
      return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值