Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)
2 seconds
256 megabytes
standard input
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.
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=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
std::vector<int> v[];
struct node
{
int w1;
int w2;
ll value;
}dp[maxn];
int main()
{
gg(n);
gg(m);
int x,y;
repd(i,,n)
{
gg(x),gg(y);
v[x].pb(y); }
repd(i,,)
{
sort(all(v[i]),greater<int>());
}
repd(i,,m)
{
dp[i]=dp[i-];
if(dp[i-].w1<=sz(v[])-&&dp[i].value<dp[i-].value+v[][dp[i-].w1])
{
dp[i].value=dp[i-].value+v[][dp[i-].w1];
dp[i].w1++;
}
if(i>)
{
if(dp[i-].w2<=sz(v[])-&&dp[i].value<dp[i-].value+v[][dp[i-].w2])
{
dp[i].value=dp[i-].value+v[][dp[i-].w2];
dp[i].w2=dp[i-].w2+;
dp[i].w1=dp[i-].w1;
}
} }
ll sum=0ll;
ll ans=0ll;
for(int i=;i<=sz(v[])&&i*<=m;++i)
{
ans=max(ans,sum+dp[m-i*].value);
if(i<sz(v[]))
{
sum+=v[][i];
}
}
printf("%lld\n",ans );
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)的更多相关文章
- Summer sell-off CodeForces - 810B (排序后贪心)
Summer holidays! Someone is going on trips, someone is visiting grandparents, but someone is trying ...
- Codeforces 808 E. Selling Souvenirs(三分)
E. Selling Souvenirs 题意: n件物品,有重量和价值,重量只有三种1,2,3.问取不超过m重量的物品的价值总和最大是多少.(n<=1e5,w<=3e5) 思路: n*w ...
- Codeforces 571B Minimization:dp + 贪心【前后相消】
题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...
- Codeforces Gym101341K:Competitions(DP)
http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...
- HDU 5811 Colosseo(拓扑排序+单调DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5811 [题目大意] 给出 一张单向图,现在将其划分成了两个部分,问划分之后的点是否分别满足按照一定 ...
- MySql下实现先排序后分组
最近在工作中遇到一个先排序后分组的需求,发现MySql不同的版本有不同的结果,特此记录. 举例:要求在shop表中查询出各类型商店中价格最高的商品. --表结构-- create table `sho ...
- Codeforces 544E K Balanced Teams (DP)
题目: You are a coach at your local university. There are nn students under your supervision, the prog ...
- Codeforces 1133E - K Balanced Teams - [DP]
题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...
- <二分查找+双指针+前缀和>解决子数组和排序后的区间和
<二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...
随机推荐
- MySQL高性能优化实战总结!
1.1 前言 MySQL对于很多Linux从业者而言,是一个非常棘手的问题,多数情况都是因为对数据库出现问题的情况和处理思路不清晰.在进行MySQL的优化之前必须要了解的就是MySQL的查询过程,很多 ...
- Android 闪烁动画
import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animatio ...
- ini (ini-parser)配置文件解析 for donet
介绍 此ini解析库适用于mono(unity3d),donet,大小在30kb左右. 开源免费:https://github.com/rickyah/ini-parser 使用示例 engine_c ...
- Linux 中数组的使用
Linux中数组本人可能用的相对较少,但是会经常遇见,也容易忘记,就顺便记录下来吧 数值类型的数组:一对括号表示数组,数组中元素之间使用“空格”来隔开 arr=(1 2 3 4 5) 字符串类型数组: ...
- AI学习---TensorFlow框架介绍[图+会话+张量+变量OP+API]
TensorFlow的数据流图 TensorFlow的结构分析: 图 + 会话 TensorFlow = 构图阶段(数据与操作的执行步骤被描绘出一个图) + 执行图阶段(使用回话执行构建好的图中操作) ...
- webpack模块定义和使用的模式
在使用webpack作为模块加载工具时,我在想module.exports的模块应该是一种什么模式,最直接地思考是单例.不太确定,所以写一个简单例子做测试. 测试代码 singleton.js: va ...
- Linux 简介(day1)
一.Linux 诞生于1991年 二.创始人:林纳斯.托瓦茨(Linus Torvalds) 三.logo:企鹅 四.Linux完整系统包括 1.Linux kernel (Linux 内核) 2.f ...
- dp P1103 书本整理 洛谷
题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发现,由于很多书的宽度不同,所 ...
- centos7下kubernetes(8.kubernetes Failover)
上一节我们运行的3个容器 其中有两个是运行在node2上一个运行在node1上,现在我们关闭node1,观察node1上的容器会发生什么变化 目前node1已经处于notready状态 现在我们查看一 ...
- eclipse导入maven项目, A resource exists with a different case: '/xxx'.
eclipse 导入maven 项目出现 这是由于你的workspace里有相同的项目, 这时在metadata里可以看到所有的project信息 只需在eclipse的package explore ...