zoukankan      html  css  js  c++  java
  • HDU

    Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering things a person can do, as it implies that they have taken the time and effort to learn about the uses and virtues of the plant and how it might benefit them, how to identify it in its native habitat or how to cultivate it in a garden, and how to prepare it as medicine. It also implies that a person has chosen to take responsibility for their own health and well being, rather than entirely surrender that faculty to another. Consider several different herbs. Each of them has a certain time which needs to be gathered, to be prepared and to be processed. Meanwhile a detailed analysis presents scores as evaluations of each herbs. Our time is running out. The only goal is to maximize the sum of scores for herbs which we can get within a limited time.

    InputThere are at most ten test cases.
    For each case, the first line consists two integers, the total number of different herbs and the time limit.
    The i -th line of the following n line consists two non-negative integers. The first one is the time we need to gather and prepare the i -th herb, and the second one is its score.

    The total number of different herbs should be no more than 100 100 . All of the other numbers read in are uniform random and should not be more than 10 9  109 .
    OutputFor each test case, output an integer as the maximum sum of scores.Sample Input

    3 70
    71 100
    69 1
    1 2

    Sample Output

    3

    题意:N个物品,以及定容量为M的容器,每个物品有自己的体积和价值,求最大价值。

    思路:就是01背包,但是M过大,每个物体的体积也是,我们我们需要优化空间。 这里用map,每次做完01背包后,去掉体积变大,价值没有变大的部分。

    好像还可以用搜索做,但是我举得没有这个巧妙。

    对于map,我们用iterator来遍历的时候,其实是按下标从小到大遍历的,与插入的顺序无关。  但如果是unordered_map,那么就与插入的顺序无关,所以这个题不能用后者。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    map<int,ll>mp,tmp;
    map<int,ll>::iterator it;
    int main()
    {
        int N,M,v,w; ll ans;
        while(~scanf("%d%d",&N,&M)){
            mp.clear(); mp[0]=0;
            for(int i=1;i<=N;i++){
                scanf("%d%d",&v,&w);
                tmp.clear();
                for(it=mp.begin();it!=mp.end();it++){
                    int x=it->first;ll y=it->second;
                    if(tmp.find(x)==tmp.end()) tmp[x]=y;
                    else tmp[x]=max(tmp[x],y);
                    if(x+v<=M) tmp[x+v]=max(tmp[x+v],y+w);
                }
                mp.clear(); ll ans=-1;
                for(it=tmp.begin();it!=tmp.end();it++)
                  if(it->second>ans){
                     mp[it->first]=it->second;
                     ans=it->second;
                }
                if(i==N) printf("%lld
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    2018-09-13 代码翻译尝试-使用Roaster解析和生成Java源码
    2018-09-10 使用现有在线翻译服务进行代码翻译的体验
    2018-09-06 Java实现英汉词典API初版发布在Maven
    2018-08-29 浏览器插件实现GitHub代码翻译原型演示
    2018-08-27 使用JDT核心库解析JDK源码后初步分析API命名
    2018-08-11 中文代码示例之Spring Boot 2.0.3问好
    2018-08-24 中文代码之Spring Boot对H2数据库简单查询
    2018-08-22 为中文API的简繁转换库添加迟到的持续集成
    2018-08-21 中文关键词替换体验页面原型
    vim打开不同的文件
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9907896.html
Copyright © 2011-2022 走看看