• Selling Souvenirs CodeForces


    E. Selling Souvenirs
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.

    This morning, as usual, Petya will come to the market. Petya has n different souvenirs to sell; ith souvenir is characterised by its weight wiand cost ci. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than m, and total cost is maximum possible.

    Help Petya to determine maximum possible total cost.

    Input

    The first line contains two integers n and m (1 ≤ n ≤ 100000, 1 ≤ m ≤ 300000) — the number of Petya's souvenirs and total weight that he can carry to the market.

    Then n lines follow. ith line contains two integers wi and ci (1 ≤ wi ≤ 3, 1 ≤ ci ≤ 109) — the weight and the cost of ith souvenir.

    Output

    Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.

    Examples
    input
    Copy
    1 1
    2 1
    output
    Copy
    0
    input
    Copy
    2 2
    1 3
    2 2
    output
    Copy
    3
    input
    Copy
    4 3
    3 10
    2 7
    2 8
    1 1
    output
    Copy
    10

    题意:一个物品个数和容量都是1e5级别的01背包问题,不过物品的花费只有1,2,3
    思路:先把物品按照花费分为3类,1,2,3,然后排序成降序,先以只取花费为1和2的物品进行DP,
    然后再用花费为3的物品去尝试更换掉1和2的物品,使总价格最大。
    还有巨巨有三分的写法,没有看懂= = ,显然太菜了。
    细节见代码。
    思路观自大佬的博客:https://blog.csdn.net/yasola/article/details/78222203

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int n,m;
    std::vector<int> v[5];
    struct node
    {
        int w1;
        int w2;
        ll value;
    }dp[maxn];
    int main()
    {
        gg(n);
        gg(m);
        int x,y;
        repd(i,1,n)
        {
            gg(x),gg(y);
            v[x].pb(y);
    
        }    
        repd(i,1,3)
        {
            sort(all(v[i]),greater<int>());
        }
        repd(i,1,m)
        {
            dp[i]=dp[i-1];
            if(dp[i-1].w1<=sz(v[1])-1&&dp[i].value<dp[i-1].value+v[1][dp[i-1].w1])
            {
                dp[i].value=dp[i-1].value+v[1][dp[i-1].w1];
                dp[i].w1++;
            }
            if(i>1)
            {
                if(dp[i-2].w2<=sz(v[2])-1&&dp[i].value<dp[i-2].value+v[2][dp[i-2].w2])
                {
                    dp[i].value=dp[i-2].value+v[2][dp[i-2].w2];
                    dp[i].w2=dp[i-2].w2+1;
                    dp[i].w1=dp[i-2].w1;
                }
            }
    
        }
        ll sum=0ll;
        ll ans=0ll;
        for(int i=0;i<=sz(v[3])&&i*3<=m;++i)
        {
            ans=max(ans,sum+dp[m-i*3].value);
            if(i<sz(v[3]))
            {
                sum+=v[3][i];
            }
        }
        printf("%lld
    ",ans );
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    JAVA学习前应该了解
    JAVA帝国的诞生
    常用的快捷方式
    MarkDown学习
    运动检测
    图像分割
    感知机
    线性判别函数
    距离
    概率密度估计笔记——非参数估计
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10278452.html
Copyright © 2020-2023  润新知