POJ 3211 Washing Clothes

简介:

题目大意:两个人同时洗一种颜色的衣服 洗完才能洗下一种颜色的衣服 求把所有种类的衣服洗完最少时间

题解:把每种颜色的衣服转化为必须装满的背包 求最接近每种颜色衣服总时间sum[i]/2的值即dp[mid]

把sum-dp[mid]累加就是答案

#include <iostream>
#include<cstring>
using namespace std;

char color[12][21],temp[21];
bool dp[100000];
int h[12][100],sum[12],t[12];
int getid(char *a)
{
    for(int i=1; i<12; i++)
        if(strcmp(color[i],a)==0)
            return i;
    return 0;
}
int main()
{
    int m,n,time;
    while(cin>>m>>n&&(m+n))
    {
        for(int i=1; i<=m; i++)
            cin>>color[i],sum[i]=t[i]=0;
        for(int j=1; j<=n; j++)
        {
            cin>>time>>temp;
            int k=getid(temp);
            h[k][t[k]++]=time;
            sum[k]+=time;
        }
        int ans=0;
        for(int i=1; i<=m; i++)
        {
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            for(int j=0; j<t[i]; j++)
                for(int k=sum[i]-h[i][j]; k>=0; k--)
                    if(dp[k])
                        dp[k+h[i][j]]=1;
            for(int j=sum[i]/2; j>=0; j--)
                if(dp[j])
                {
                    ans+=(sum[i]-j);
                    break;
                }     //也可以像底下这么写
            /* for(int j=sum[i]%2?sum[i]/2+1:sum[i]/2; j<=sum[i]; j++)
                 if(dp[j])
                 {
                     ans+=j;
                     break;
                 }*/

        }
        cout<<ans<<endl;
    }
    return 0;
}


 

目录
相关文章
|
测试技术
POJ 1001
此题用最朴素的思路实现即可,需模拟加法器,乘法器,最烦人的地方是特殊情形,如末位是小数点(12.^2=144,取小数点),整数末位是0(100^2=10000),0次幂,测试用例可能超出题目中说的范围,可能包含0次幂(100.0^0=0, 0.10^1=0.1)。
729 0
poj 1455
Description n participants of > sit around the table. Each minute one pair of neighbors can change their places.
599 0
poj 2299 求逆序数
http://poj.org/problem?id=2299 #include using namespace std; int aa[500010],bb[500010]; long long s=0; void merge(int l,int m,int r) { ...
767 0
|
人工智能 BI
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
938 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
840 0
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
827 0
|
机器学习/深度学习 Windows
|
人工智能 机器学习/深度学习

热门文章

最新文章