• 3 B. Lorry


    题目大意:给你两种物品,每种物品有一个价值和花费,花费只有两种,一种花费为 1, 一种花费为2.、
    给你一个背包容量为v, 求当前容量下所能达到的最大价值。
    ==========================================================
    
    
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cstdlib>
    using namespace std;
    typedef __int64 LL;
    const LL INF = 0xffffff;
    const int maxn = 100005;
    const LL MOD = 1e9+7;
    
    struct node
    {
        int cost;///花费
        int value;///价值
        int index;///下标
        bool friend operator < (node A, node B)
        {
            return A.value > B.value;///按照单价进行排序
        }
    }One[maxn], Tow[maxn], P;
    int sum[maxn] = {0}, ans[maxn];
    int main()
    {
        int n, Cost;
        int num1 = 1, num2 = 1;///Last 最后一个价值为 1 物品的下标。
        scanf("%d %d", &n, &Cost);
    
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d",&P.cost, &P.value);
            P.index = i;
            if(P.cost == 1)
                One[num1 ++] = P;
            else
                Tow[num2 ++] = P;
        }
        sort(One+1, One + num1+1);
        sort(Tow+1, Tow + num2+1);
        Tow[0].cost = 0, Tow[0].value = 0;
        for(int i=1; i< num1; i++)
            sum[i] += sum[i-1] + One[i].value;
    
        int Index = 0, Max = 0, Temp, CurValue = 0, CurCost = 0;
        for(int i=0; i< num2; i++)
        {
            CurValue += Tow[i].value;
            CurCost += Tow[i].cost;
            if(CurCost > Cost)
                break;
    
            if(Cost - CurCost >= num1-1)
                Temp =  CurValue + sum[num1-1];
            else
                Temp =  CurValue + sum[Cost - CurCost];
    
            if(Temp > Max)
            {
                Max = Temp;
                Index = i;
            }
        }
    
        printf("%d
    ", Max);
        int k = 0;
        for(int i=1; i<=Index; i++)
            ans[k ++] = Tow[i].index;
    
        for(int i=1; i<=(Cost-Index*2)&& i<num1; i++)
            ans[k ++] = One[i].index;
    
        for(int i=0; i<k-1 ;i++)
            printf("%d ", ans[i]);
        if(k != 0)
            printf("%d
    ", ans[k-1]);
    
        return 0;
    }
    /*
    10 10
    1 14
    2 15
    2 11
    2 12
    2 9
    1 14
    2 15
    1 9
    2 11
    2 6
    */
  • 相关阅读:
    Teradata 奇淫技巧
    Java
    搬砖
    js 判断2个对象的值是否相等
    搬砖
    UnsupportedClassVersionError: JVMCFRE003 commons/text/StringEscapeUtils
    杂记
    java查找最新文件
    搬砖
    搬砖
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/4844846.html
Copyright © 2020-2023  润新知