zoukankan      html  css  js  c++  java
  • poj 3211 Washing Clothes

    http://poj.org/problem?id=3211

    一个背包的简单应用。

    我们可以这样想,对于同一种颜色的衣服,把他们的总时间t算出来,那么做一个容量为t/2的背包,背包里放的是这种颜色的衣服,重量是洗这件衣服的时间,价值也是洗

    这件衣服的时间,那么洗这种颜色的衣服的所需最小时间就是 max(t-dp【t/2】,dp【t/2】)

    用g++交才能过,c++不行,这是怎么个回事??

    View Code
    #include<iostream>
    #include<string.h>
    #include<string.h>
    #include<stdio.h>
    #include<vector>
    #include<map>
    #define maxn 200
    using namespace std;
    int t[maxn];
    int close[maxn][maxn];
    int dp[maxn*maxn];
    int c,m;
    map<string,int >v;
    int main()
    {
        string s;
        int a;
        while(cin>>c>>m)
        {
            memset(t,0,sizeof(t));
            memset(close,0,sizeof(close));
            v.clear();//要清空map
            if(!(c+m))break;
            for(int i=1;i<=c;i++)
            {
                cin>>s;
                v[s]=i;//用map,给每一种颜色编号,
            }
            for(int i=1;i<=m;i++)
            {
                cin>>a>>s;
                t[v[s]]+=a;//这种颜色的衣服总时间要增加
       close[v[s]][++close[v[s]][0]]=a;
    //二维数组,第一唯是颜色序号,第二唯放的是每一件这种颜色衣服的间
            }
            int ans=0;
            for(int i=1;i<=c;i++)
            {
                memset(dp,0,sizeof(dp));
            for(int j=1;j<=close[i][0];j++)//数目是这种颜色衣服的数目
                {
               for(int k=t[i]/2;k>=close[i][j];k--)//容量是总时间的一半
                    {
                  dp[k]=max(dp[k],dp[k-close[i][j]]+close[i][j]);//背包
                    }
                }
       ans+=max(dp[t[i]/2],t[i]-dp[t[i]/2]);
    //求取最大值,该值是洗这种颜色衣服所需的最小时间
            }
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    sys_refcursor vs ref cursor in oracle
    Oracle-cursor动态游标
    游标(cursor)--显式游标&隐式游标、游标四个属性、循环遍历
    PL/SQL IF CASE
    python字符串的encode和decode
    python中raw_input()与input()
    Emacs显示行号
    Python爬虫——抓取糗百段子
    Python代码一定要对齐
    Python标准库内置函数——hasattr
  • 原文地址:https://www.cnblogs.com/cs1003/p/2662251.html
Copyright © 2011-2022 走看看