在不大于某解内n个数最多可以组合成多大的数

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

做这道题的时候很迷。。。原先做出了多重部分和的题,然后发现这题其实差不多,只是非多重,于是我尝试优化过的一维数组的方法做,结果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;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值