Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心
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 wi and 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 ofith 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
题目大意就是0-1背包问题,然后看着逆天的数据范围,O(nm)的算法怕是去卡评测机的。
只能另想出路了。注意到每个物品的最大重量为3,突破口应该就在这儿。
先按照物品的重量分类,排序(从大到小,因为同种重量选价值大不会更劣),求前缀和。
考虑枚举重量为3的物品选择的物品的数量。考虑对剩下物品dp。不难证明容量+1后对策略的影响只有:
- 加入一个重量为1的物品。
- 拿走一个重量为1的物品加入一个重量为2的物品。
(大概就是因为每次改变的物品重量和不会超过2,如果改变的物品重量和超过2,那么两边一定存在1个重量和为2的物品集合,把现在这个集合里的替换为新的集合中会更优,这样会矛盾)
不过暴力记下每种重量的物品选择的数量也可以dp。讨论一下发现和这个操作是类似的。
如果将两个物品拿来dp,然后最后枚举状态,计算第三种物品需要的量,再根据前缀和快速计算。如果用重量为1和2的物品来dp,经过一番考虑决定用f[i]表示当前总共装了质量为i的物品最大的价值和和两种物品各用的数量(对,没有看错,用的是一个结构体来存的)。
为什么这么做是正确的?
首先我们按照从大到小排序,如果说一个重量为2的物品比两个重量为1的物品优,那么对于这个状态以及这之前的状态,两个质量为1的物品都不会更优,并且会被这个重量为2的物品替换掉,也就是说最优的时候有确定的两种物品的数量,即使是两个重量为1的物品之和和一个重量为2的物品价值和相等,也不会影响。转移的时候我肯定希望能放的尽量大,所以转移的时候只需要枚举是装入下一个重量为1的物品还是重量为2的物品。
最后计算一下答案,取max即可。
-->
Code
/**
* Codeforces
* Problem#808E
* Accepted
* Time:31ms
* Memory:9400k
*/
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#include<stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long typedef class Data {
public:
LL val;
int s1, s2; Data():val(), s1(), s2() { }
}Data; int n, m;
Data* f;
int cnt[] = {, , , };
int vs[][];
LL s[][]; inline void init() {
readInteger(n);
readInteger(m);
f = new Data[(const int)(m + )];
for(int i = , v, w; i <= n; i++) {
readInteger(w);
readInteger(v);
vs[w][++cnt[w]] = v;
}
} boolean cmpare (const int& a, const int& b) {
return a > b;
} LL res = ; inline void solve() {
for(int i = ; i <= ; i++) {
sort(vs[i] + , vs[i] + cnt[i] + , cmpare);
s[i][] = ;
for(int j = ; j <= cnt[i]; j++)
s[i][j] = s[i][j - ] + vs[i][j];
} for(int i = ; i <= m; i++) {
if(f[i - ].s1 < cnt[] && f[i].val < f[i - ].val + vs[][f[i - ].s1 + ]) {
f[i].val = f[i - ].val + vs[][f[i - ].s1 + ];
f[i].s1 = f[i - ].s1 + , f[i].s2 = f[i - ].s2;
}
if(i >= && f[i - ].s2 < cnt[] && f[i].val < f[i - ].val + vs[][f[i - ].s2 + ]) {
f[i].val = f[i - ].val + vs[][f[i - ].s2 + ];
f[i].s2 = f[i - ].s2 + , f[i].s1 = f[i - ].s1;
}
} for(int i = ; i <= m; i++)
if((m - f[i].s1 - f[i].s2 * ) / <= cnt[])
smax(res, f[i].val + s[][(m - f[i].s1 - f[i].s2 * ) / ]);
else
smax(res, f[i].val + s[][cnt[]]);
printf(Auto, res);
} int main() {
init();
solve();
return ;
}
Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心的更多相关文章
- Educational Codeforces Round 21 Problem D(Codeforces 808D)
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into t ...
- Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案
Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...
- Educational Codeforces Round 21 Problem A - C
Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...
- Educational Codeforces Round 21
Educational Codeforces Round 21 A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...
- Educational Codeforces Round 32 Problem 888C - K-Dominant Character
1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation —— 贪心 + 字符串
题目链接:http://codeforces.com/problemset/problem/632/C C. The Smallest String Concatenation time limit ...
- Codeforces Round #524 (Div. 2) codeforces 1080A~1080F
目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...
- Educational Codeforces Round 21 D.Array Division(二分)
D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- Educational Codeforces Round 21(A.暴力,B.前缀和,C.贪心)
A. Lucky Year time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
随机推荐
- CCCC 月饼
https://www.patest.cn/contests/gplt/L2-003 题解:按平均值贪心. 坑:有一个样例卡住了,是因为 while (i<=n&&x - bs[ ...
- Oracle备份恢复之数据库备份、还原、恢复理论
备份 冷备:关闭数据库并进行数据库物理文件的拷贝过程. 热备:数据库处于open阶段时的备份,通过指令将数据库文件头锁定,然后进行物理系统拷贝,然后通过指令解冻数据文件头,解冻后通过日志文件和undo ...
- Python面向对象:类、实例与访问限制
首先记录下面向对象的名词: 对象:python万物皆对象,程序设计的东西在对象上体现. 类:具有相同属性和行为的对象的集合. 消息:各个对象之间通过消息相互联系. 方法:对象功能实现的过程. 封装:把 ...
- docker and ssh issues
docker run -i -t qcdatainc/centos-jdkyum update yum install openssh-server ssh -vvv -p 222 jenkins@1 ...
- 【Git 使用笔记】第三部分:多分支开发
###举例仓库 仓库地址A:git@gitlab.54php.cn:guowei/demos.git 仓库地址B:git@gitlab.54php.cn:infra/demos.git 开发人员仓库C ...
- Alignment--POJ1836
Description In the army, a platoon is composed by n soldiers. During the morning inspection, the sol ...
- linux下ssh远程登录/scp远程复制文件/rsync远程同步命令的自动登录
最近需要写一个脚本备份各个服务器上的程序到一个指定服务器上,本来以为查查rsync命令的使用321就能搞定,结果rsync命令要支持自动登 录还是要配置服务和参数,又不确定网上说的配置的行不行,因为都 ...
- Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- laravel教程入门笔记
安装laravel框架 1.安装命令 composer create-project --prefer-dist laravel/laravel ytkah ytkah表示文件夹名,如果不写的话自动会 ...
- [vue]计算和侦听属性(computed&watch)
先看一下计算属性 vue只有data区的数据才具备响应式的功能. 计算和侦听属性 - v-text里可以写一些逻辑 <div id="example"> {{ mess ...