zoukankan      html  css  js  c++  java
  • 牛客网-Beautiful Land 【01背包 + 思维】

    链接:https://www.nowcoder.com/acm/contest/119/F
    来源:牛客网

    Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the value of the tree. Also each of the trees have an area cost, it means we need to cost ci area of land to plant.
    We know the cost and the value of all the trees. Now HUSTers want to maximize the value of trees which are planted in the land. Can you help them?

    输入描述:

    There are multiple cases.
    The first line is an integer T(T≤10), which is the number of test cases.
    For each test case, the first line is two number n(1≤n≤100) and C(1≤C≤10
    8
    ), the number of seeds and the capacity of the land. 
    Then next n lines, each line contains two integer c
    i
    (1≤c
    i
    ≤10
    6
    ) and v
    i
    (1≤v
    i
    ≤100), the space cost and the value of the i-th tree.

    输出描述:

    For each case, output one integer which means the max value of the trees that can be plant in the land.
    示例1

    输入

    1
    3 10
    5 10
    5 10
    4 12

    输出

    22
    这题如果硬算,v太大。
    所以要转换下思维。
    普通的01背包求的是相同c下最大的v
    可以转换为相同v的情况下最小的c
    这样就不会超时了。
     
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<map>
     8 #define max(a,b)(a>b?a:b)
     9 #define min(a,b)(a<b?a:b)
    10 typedef long long ll;
    11 using namespace std;
    12 const int N  = 10010;
    13 const ll inf = 0x3f3f3f3f3f3f3f3f;
    14 ll dp[N],w[N];  ///此时的dp[i]表示的是;价值为i时的最小容量为dp[i];
    15 int v[N];
    16 
    17 int main()
    18 {
    19     int T,i,n,sum;
    20     ll V;
    21     scanf("%d",&T);
    22     while(T--)
    23     {
    24         scanf("%d%lld",&n,&V);
    25         sum=0;
    26         for(i=1;i<=n;i++)
    27         {
    28             scanf("%lld%d",&w[i],&v[i]);
    29             sum=sum+v[i];
    30         }
    31 
    32         memset(dp,100000010,sizeof(dp)); ///要求最小容量,初始化为最大值;
    33         dp[0]=0;
    34         for(i=1;i<=n;i++)
    35         {
    36             for(int j=sum;j>=v[i];j--)
    37                 dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
    38         }
    39 
    40         for(i=sum;i>=0;i--)
    41         {
    42             if(dp[i]<=V)
    43             {
    44                printf("%d
    ",i); ///此处输出i,即为满足条件的最大价值
    45                break;
    46             }
    47         }
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    vim的script、function及command
    Vim中如何移动光标
    command模式下命令的匹配及help内容的搜索
    为什么vim编辑模式下ctrl-w可以前向删除单词及按键映射的展开
    gcc如何实现C++中函数auto返回类型推导
    sqlserver 列转行
    【java笔记】可变长参数(...)
    【c#笔记】可变长参数(params)
    【c#笔记】c#与java的差异:接口定义实现
    【java笔记】Calendar类的陷阱
  • 原文地址:https://www.cnblogs.com/zmin/p/8997878.html
Copyright © 2011-2022 走看看