标签:petya xmlns ret can lower .com queue hat har
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.
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.
Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.
1 1
2 1
0
2 2
1 3
2 2
3
4 3
3 10
2 7
2 8
1 1
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), ‘\0‘, 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\n",ans ); return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ‘ ‘ || ch == ‘\n‘); 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‘; } } }
Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)
标签:petya xmlns ret can lower .com queue hat har
原文地址:https://www.cnblogs.com/qieqiemin/p/10278452.html