zoukankan      html  css  js  c++  java
  • Codeforces 3B

    B. Lorry

    time limit per test
    2 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

    Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.

    Input
    The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 1051 ≤ v ≤ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
    Output

    In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.

    Examples
    input
    3 2
    1 2
    2 7
    1 3
    output
    7
    2

    题意:给定n个物品的体积和价值(体积只可能为1或2)和一个体积为v的背包,问你怎样在背包不超载的情况下得到最大的价值。把最大价值和选定的物品编号输出。

    题解:贪心。将物品体积为1和为2的分别按价值关键字从大到小排序,然后维护排好序后两列物品价值的前缀和,每次枚举体积为1的物体选的个数,从而推算体积为2的物体最多能选几个,然后直接用两者枚举到的个数的前缀和相加与Max比较,更新答案即可。

     1 #include<bits/stdc++.h>
     2 const int N=100005;
     3 using namespace std;
     4 struct node{
     5     int p,id;
     6 }a[N],b[N];
     7 int n,v,t,sz1,sz2,cnt=0,cnt1=0,cnt2=0,Max=-1,sum1[N],sum2[N],ans[N];
     8 bool cmp(node a,node b){
     9     return a.p>b.p;
    10 }
    11 int main()
    12 {
    13     scanf("%d%d",&n,&v);
    14     for (int i=1;i<=n;++i)
    15     {
    16         scanf("%d",&t);
    17         if (t==1)
    18         {
    19             scanf("%d",&a[++cnt1].p);
    20             a[cnt1].id=i;
    21         }
    22         else
    23         {
    24             scanf("%d",&b[++cnt2].p);
    25             b[cnt2].id=i;
    26         }
    27     }
    28     sort(a+1,a+cnt1+1,cmp);
    29     sort(b+1,b+cnt2+1,cmp);
    30     sum1[0]=sum2[0]=0;
    31     for (int i=1;i<=cnt1;++i)
    32     sum1[i]=sum1[i-1]+a[i].p;
    33     for (int i=1;i<=cnt2;++i)
    34     sum2[i]=sum2[i-1]+b[i].p;
    35     for (int i=0;i<=cnt1;++i)
    36     {
    37         if (i>v) break;
    38         if (sum1[i]+sum2[min((v-i)/2,cnt2)]>Max)
    39         {
    40             Max=sum1[i]+sum2[min((v-i)/2,cnt2)];
    41             sz1=i; sz2=min((v-i)/2,cnt2);
    42         }
    43     }
    44     if (Max==-1)
    45     {
    46         printf("0
    ");
    47         return 0;
    48     }
    49     printf("%d
    ",Max);
    50     for (int i=1;i<=sz1;++i)
    51     ans[++cnt]=a[i].id;
    52     for (int i=1;i<=sz2;++i)
    53     ans[++cnt]=b[i].id;
    54     for (int i=1;i<=cnt;++i)
    55     printf("%d ",ans[i]);
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    Mybatis和Spring整合也是能用BatchExecutor的
    与Spring整合的Mybatis没法真正使用BatchExecutor
    Mybatis和Spring整合后sqlsession啥时候关闭的
    Mybatis和Spring的整合原理
    Mybatis是怎么执行一条语句的
    8.11查询结果排序
    8.10、11(select分组和过滤)()
    8.7、8、9(select语句基本用法)(select语句基本运算符)(select聚合查询)
    8.4SQL(DML数据操作语言)-(insert插入数据)(updata更新数据),(delete数据)
    8.2数据库DDL语言(即数据库定义语言)(命名规则以及数据类型)
  • 原文地址:https://www.cnblogs.com/zk1431043937/p/7587962.html
Copyright © 2011-2022 走看看