Codeforces 808E Selling Souvenirs【思维+Dp】
来源:互联网 发布:ubuntu llmp 编辑:程序博客网 时间:2024/05/17 23:22
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 thanm, 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
题目大意:
和01背包相似,现在有N个物品,背包容量为M.每个物品有两个属性,分别是重量和价值。
特殊之处在于重量的数据范围是1~3.
思路:
1、观察到物品重量的范围是1~3.
那么我们不妨先将所有物品分成三类,然后对于每一类物品按照价值从大到小排序。
我们肯定贪心的想要取价值高的。
2、那么我们进行一个三元组Dp,设定Dp【i】表示容量为i的背包,其状态为:(重量为1的物品已经购买的数量,重量为2的物品已经购买的数量,最高价值);
那么我们对其Dp一下,那么对应再枚举购买重量为3的物品的个数即可。
3、不太明白为什么不能对三种物品都Dp搞一个四元组Dp..一直Wa 45.....................留给之后没事情做的时候去想把。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;#define ll __int64struct node{ ll val; ll x,y,z;}dp[300050];ll sum[300050];vector<ll>a[5];int main(){ ll n,m; while(~scanf("%I64d%I64d",&n,&m)) { memset(sum,0,sizeof(sum)); for(ll i=1;i<=4;i++)a[i].clear(); for(ll i=0;i<n;i++) { ll x,y; scanf("%I64d%I64d",&x,&y); a[x].push_back(y); } for(ll i=1;i<=3;i++) { sort(a[i].begin(),a[i].end()); reverse(a[i].begin(),a[i].end()); if(i==3) { for(int j=0;j<a[i].size();j++) { if(j==0)sum[j]=a[i][j]; else sum[j]=sum[j-1]+a[i][j]; } } } memset(dp,0,sizeof(dp)); ll output=0; for(ll i=1;i<=m;i++) { dp[i]=dp[i-1]; if(i>=1) { if(dp[i-1].x<a[1].size()&&dp[i-1].val+a[1][dp[i-1].x]>dp[i].val) { dp[i].val=dp[i-1].val+a[1][dp[i-1].x]; dp[i].x=dp[i-1].x+1; dp[i].y=dp[i-1].y; dp[i].z=dp[i-1].z; } } if(i>=2) { if(dp[i-2].y<a[2].size()&&dp[i-2].val+a[2][dp[i-2].y]>dp[i].val) { dp[i].val=dp[i-2].val+a[2][dp[i-2].y]; dp[i].x=dp[i-2].x; dp[i].y=dp[i-2].y+1; dp[i].z=dp[i-2].z; } } output=max(output,dp[i].val); } for(int j=0;j<a[3].size();j++) { if(m-(j+1)*3>=0) output=max(output,dp[m-(j+1)*3].val+sum[j]); } printf("%I64d\n",output); }}
- Codeforces 808E Selling Souvenirs【思维+Dp】
- codeforces Round 21 808E. Selling Souvenirs 【dp好题】
- CodeForces 808E Selling Souvenirs(三分法/单调优化dp)
- Codeforces 808E Selling Souvenirs 题解
- [杂题 贪心] Codeforces 808E. Selling Souvenirs
- 808E. Selling Souvenirs***
- CF 808E. Selling Souvenirs
- 【codeforces 808E】【Selling Souvenirs】【贪心】【动态规划】
- E. Selling Souvenirs
- Codeforces 808E 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 340E Iahub and Permutations【思维+错排Dp】
- Codeforces 315E Sereja and Subsequences【思维+Dp】
- Codeforces 239E World Eater Brothers【思维+树形Dp】
- Codeforces 509E(思维)
- POJ 1383 Labyrinth 笔记
- Android BlueTooth搜索附近和连接
- 【教程】将面板和其他Swing组件绘制到PDF文档
- Apache+Tomcat实现负载均衡及集群(session同步)--一、前言
- 抽象类与接口
- Codeforces 808E Selling Souvenirs【思维+Dp】
- 深入了解MyBatis参数
- 最长公共子序列
- ThinkPHP 3.23 框架执行流程分析
- JAVA数据结构:简单排序(冒泡,选择,插入)
- 在osChina上生成ssh公钥的步骤
- 一致性哈希的浅析
- Nvidia-OpenCL-SDK-Code-Samples的学习[3]
- Linux学习