CodeForces 808E Selling Souvenirs(三分法/单调优化dp)
来源:互联网 发布:软件开发很难吗 编辑:程序博客网 时间:2024/06/06 12:42
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 weightwi and costci. 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 andm (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 andci (1 ≤ wi ≤ 3,1 ≤ ci ≤ 109) — the weight and the cost ofith souvenir.
Print one number — maximum possible total cost of souvenirs that Petya can carry to the market.
1 12 1
0
2 21 32 2
3
4 33 102 72 81 1
10
#include<bits/stdc++.h>using namespace std;struct Node{long long val;int a,b;} dp[310000];vector<int> w1,w2,w3;long long s[110000];int n,m;bool cmp(int a,int b){return a>b;}int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);if (x==1) w1.push_back(y);if (x==2) w2.push_back(y);if (x==3) w3.push_back(y);}sort(w1.begin(),w1.end(),cmp);sort(w2.begin(),w2.end(),cmp);sort(w3.begin(),w3.end(),cmp);for(int i=0;i<w3.size();i++)s[i+1]=s[i]+w3[i];dp[0].val=dp[0].a=dp[0].b=0;long long ans=-1e16;for(int i=1;i<=m;i++)//枚举质量为3的物品取的个数{dp[i]=dp[i-1]; int a=dp[i-1].a;if (a<w1.size() && dp[i-1].val+w1[a]>dp[i].val)//取一个质量为1的物品{dp[i].val=dp[i-1].val+w1[a];dp[i].a++;}if (i>1){int b=dp[i-2].b;if (i>1 && b<w2.size() && dp[i-2].val+w2[b]>dp[i].val)//取一个质量为2的物品{dp[i]=dp[i-2]; dp[i].b++;dp[i].val=dp[i-2].val+w2[b];}}ans=max(ans,dp[i].val+s[min((int) w3.size(),(m-dp[i].a-2*dp[i].b)/3)]);//把质量为3的物品的价值加上}ans=max(ans,dp[0].val+s[min((int) w3.size(),(m-dp[0].a-2*dp[0].b)/3)]);printf("%I64d\n",ans);return 0;}
#include<bits/stdc++.h>using namespace std;long long s[110000][3];vector<int> w[3];int n,m,i;bool cmp(int a,int b){return a>b;}long long f(int x){return s[x][1]+s[min(m-i*3-x*2,(int) w[0].size())][0];}int main(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);w[x-1].push_back(y);}long long ans=0;sort(w[0].begin(),w[0].end(),cmp);sort(w[1].begin(),w[1].end(),cmp);sort(w[2].begin(),w[2].end(),cmp);for(int i=0;i<3;i++)for(int j=0;j<w[i].size();j++)s[j+1][i]=s[j][i]+w[i][j];int l,r,lmid,rmid;for(i=0;i<=min((int) w[2].size(),m/3);i++){l=0,r=min((m-i*3)/2,(int) w[1].size());while (l<r-1){lmid=(l+r)>>1;rmid=(lmid+r)>>1;if (f(lmid)>f(rmid)) r=rmid; else l=lmid;}ans=max(ans,max(f(l),f(r))+s[i][2]);}printf("%I64d\n",ans);return 0;}
- CodeForces 808E Selling Souvenirs(三分法/单调优化dp)
- Codeforces 808E Selling Souvenirs【思维+Dp】
- codeforces Round 21 808E. Selling Souvenirs 【dp好题】
- Codeforces 808E Selling Souvenirs 题解
- [杂题 贪心] Codeforces 808E. Selling Souvenirs
- 808E. Selling Souvenirs***
- CF 808E. Selling Souvenirs
- 【codeforces 808E】【Selling Souvenirs】【贪心】【动态规划】
- Codeforces 808E Selling Souvenirs(花费是倍数关系的背包)
- E. Selling Souvenirs
- 01背包 Educational Codeforces Round 21 E. Selling Souvenirs
- Educational Codeforces Round 21 E. Selling Souvenirs 三分, 贪心
- codeforcodeforces 808E——Selling Souvenirs(动态规划)官方题解详解
- Codeforecs 808E Selling Sovenirs 枚举+DP(超大背包)
- Codeforces 373E Watching Fireworks is Fun【思维+单调队列优化Dp+滚动数组】
- UVa 12265:Selling Land(单调栈)
- Codeforces 777E(贪心+单调栈)
- Codeforces 797F Mice And Holes DP(单调队列优化)
- 趣文:Python程序员的进化史
- Android11
- google webRTC服务器搭建(CentOS7.3)
- NIO
- Android12
- CodeForces 808E Selling Souvenirs(三分法/单调优化dp)
- 画出一个时钟
- Hive分析窗口函数(二) NTILE,ROW_NUMBER,RANK,DENSE_RANK
- Android13
- SQL Server2008新实例远程数据库链接问题(sp_addlinkedserver)
- hadoop的安装与配置方法详解
- 泛型
- squid与bond0
- Leetcode 89(Java)