Codeforces Gym 101002 H. Jewel Thief 题解

题意

类似于一个背包,空间为$M$,有$N$个物品,第$i$个物品体积为$w_i$,价值为$c_i$,求价值之和的最大值。
其中,$1 \leq n \leq 100000$,$1\leq m \leq 300000$,$1\leq w_i \leq 3$,$1\leq c_i \leq {10}^9$

思路

首先注意到$n,m$非常大,所以普通的背包是肯定不行的,那么考虑从小数据(每个物品的体积)入手。
发现:体积只有$1,2,3$三种可能,那么就分类讨论就好了。
首先,把所有物品按照体积分为$3$类。
然后每一类的物品按照价值排序(从大到小)(因为同种体积,价值越大越好)。
设$f[i]$表示空间为$i$的目前价值之和的最大值。

#include<algorithm>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define int long long
using namespace std;
inline int read(){
    int res=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
    return res*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x+'0');
    else{
        write(x/10);
        putchar(x%10+'0');
    }
}
//queue<int> q;
//set<int> s;
//priority_queue<int> q1;
//priority_queue<int,vector<int>,greater<int> > q2;
//list<int> l;
//stack<int> s;
int n,m,f[300010],ans=0;
vector<int> g[5];
int dp_g[5][300010];
struct node{
    int w,c;
}a[300010];
bool cmp(node qx,node qy){
    return (double)((double)qx.c/(double)qx.w)>(double)((double)qy.c/(double)qy.w);
}
signed main(){
//  freopen("B.in","r",stdin);freopen("B.out","w",stdout);
    n=read();m=read();
    for(int i=1;i<=n;i++){
        a[i].w=read();a[i].c=read();
        g[a[i].w].push_back(a[i].c);
    }
    for(int i=1;i<=3;i++) sort(g[i].begin(),g[i].end());
    for(int i=1;i<=3;i++) dp_g[i][0]=g[i].size();
    for(int i=0;i<=m;i++){
        for(int j=1;j<=3;j++){
            if(dp_g[j][i]!=0){
                if(f[i]+g[j][dp_g[j][i]-1]==f[i+j]){
                    if(dp_g[1][i]<=dp_g[1][i+j]){
                        dp_g[1][i+j]=dp_g[1][i];
                        dp_g[2][i+j]=dp_g[2][i];
                        dp_g[3][i+j]=dp_g[3][i];
                        dp_g[j][i+j]--;
                    }
                    if(dp_g[1][i]-1==dp_g[1][i+j]&&dp_g[2][i]<dp_g[2][i+j]){
                        dp_g[1][i+j]=dp_g[1][i];
                        dp_g[2][i+j]=dp_g[2][i];
                        dp_g[3][i+j]=dp_g[3][i];
                        dp_g[j][i+j]--;
                    }
                }
                if(f[i]+g[j][dp_g[j][i]-1]>f[i+j]){
                    f[i+j]=f[i]+g[j][dp_g[j][i]-1];
                    dp_g[1][i+j]=dp_g[1][i];
                    dp_g[2][i+j]=dp_g[2][i];
                    dp_g[3][i+j]=dp_g[3][i];
                    dp_g[j][i+j]--;
                }
            }
        }
        ans=max(ans,f[i]);
    }
    write(ans);putchar('\n');
    return 0;
}

评论

  1. 2年前
    2019-5-18 16:51:52

    Join some of parents and share your content and develop a relationship by bloggers.
    This is the same goal get for particular customers.
    That last thought is actually definitely an article by itself. https://918kiss.poker/casino-games/71-joker123

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇