http://codeforces.com/contest/808/problem/E

不理解为什么dp = {cost, cnt1, cnt2}可以

而dp = {cost, cnt1, cnt2, cnt3}不可以

上面那个不可以的例子是:

但是这个dp是可行的,只是还有一个更新没实现起来。

dp[i] = dp[i - 2] - (花费为1的元素) + (花费为3的元素)

原来可以按1选择的数目分类,分成奇偶,然后贪心。集训队rank1的zk教我的。cf 2200+ Orz

下面评论有详解

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#include <time.h>
const int maxn = + ;
int a[][maxn];
int num[];
struct Node {
LL val;
int cnt;
} dp[maxn], dp2[maxn];
bool cmp(int x, int y) {
return x > y;
}
LL sum[maxn];
LL odd[maxn], even[maxn];
LL sum3[maxn];
void work() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
int lenOdd = , lenEven = ;
for (int i = ; i <= n; ++i) {
int id, x;
scanf("%d", &id);
scanf("%d", &a[id][++num[id]]);
if (id == ) {
odd[++lenOdd] = even[++lenEven] = a[id][num[id]];
}
}
for (int i = ; i <= ; ++i) {
sort(a[i] + , a[i] + + num[i], cmp);
} for (int i = ; i <= num[]; ++i) {
sum3[i] = sum3[i - ] + a[][i];
} for (int i = ; i <= num[]; i += ) {
if (i + > num[]) break;
even[++lenEven] = a[][i] + a[][i + ];
}
sort(even + , even + + lenEven, cmp);
for (int i = ; i <= lenEven; ++i) {
even[i] = even[i] + even[i - ];
}
LL ans = ;
for (int i = ; i <= m; ++i) { //even个,暴力枚举用在另外两个的总量是多少
LL res = even[min(lenEven, i / )];
LL res2 = sum3[min((m - i) / , num[])];
ans = max(res + res2, ans);
} for (int i = ; i <= num[]; i += ) {
if (i + > num[]) break;
odd[++lenOdd] = a[][i] + a[][i + ];
}
sort(odd + , odd + + lenOdd, cmp);
for (int i = ; i <= lenOdd; ++i) {
odd[i] = odd[i - ] + odd[i];
}
LL ans2 = ;
for (int i = ; i <= m - ; ++i) {
LL res = odd[min(lenOdd, i / )];
LL res2 = sum3[min(num[], (m - i - ) / )];
ans2 = max(ans2, res + res2);
}
cout << max(ans, ans2 + a[][]) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

E. Selling Souvenirs 不会做的更多相关文章

  1. Codeforces 808 E. Selling Souvenirs(三分)

    E. Selling Souvenirs 题意: n件物品,有重量和价值,重量只有三种1,2,3.问取不超过m重量的物品的价值总和最大是多少.(n<=1e5,w<=3e5) 思路: n*w ...

  2. Selling Souvenirs CodeForces - 808E (分类排序后DP+贪心)

    E. Selling Souvenirs time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Educational Codeforces Round 21E selling souvenirs (dp)

    传送门 题意 给出n个体积为wi,价值为ci的物品,现在有一个m大的背包 问如何装使得最后背包内的物品价值最大,输出价值 分析 一般的思路是01背包,但n*v不可做 题解的思路 We can iter ...

  4. 【dp】E. Selling Souvenirs

    http://codeforces.com/contest/808/problem/E 题意:给定n个重量为可能1,2,3的纪念品和各自的价值,问在背包总重量不超过m的条件下总价值最大为多少. 其中1 ...

  5. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  6. CF808E Selling Souvenirs

    题目链接: http://codeforces.com/contest/808/problem/E 题目大意: Petya 有 n 个纪念品,他能带的最大的重量为 m,各个纪念品的重量为 wi,花费为 ...

  7. Educational Codeforces Round 21

    Educational Codeforces Round 21  A. Lucky Year 个位数直接输出\(1\) 否则,假设\(n\)十进制最高位的值为\(s\),答案就是\(s-(n\mod ...

  8. #YCB#待做题目与填坑资料

    各种填坑资料(qwq) 主席树(by YL)戳 树套树(by ZSY)戳 不要问我这些题咋来的(查大佬的水表呗) 题目列表: [HDU5977]Garden of Eden [BZOJ2752][HA ...

  9. codeforces选做

    收录了最近本人完成的一部分codeforces习题,不定期更新 codeforces 1132E Knapsack 注意到如果只使用某一种物品,那么这八种物品可以达到的最小相同重量为\(840\) 故 ...

随机推荐

  1. exec 和 spawn 的区别

    参考资料: difference-between-spawn-and-exec-of-node-js-child_process process_child 最近在用nodejs 的child_pro ...

  2. Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关

    什么是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,以支持ASP.NET.ASP.NET CORE.PHP为特色,同时具备反向代理.入侵检测等重要功能.可以这样说,J ...

  3. C语言中的文件操作

    按照字符的方式读取文件 按照行的方式读取文件 按照数据块的方式读取文件 按照格式化的方式读取文件 文件分类 记录文件:具有一定的结构记录组成,分为定长和不定长两种方式 流式文件:按照一个字符一个字符( ...

  4. 使用TortoiseGit同步代码到github远程仓库

    1.clone github上的代码仓库的URL 可以用HTTPS,SSH, or Subversion 2.同步push 到远程仓库时 要用 SSH地址,同生成SSH private key ,在g ...

  5. java面试题06

    题目: 数据库 1. 表名:g_cardapply 字段(字段名/类型/长度): g_applyno varchar 8://申请单号(关键字) g_applydate bigint 8://申请日期 ...

  6. request.getSession()方法的应用

    request.getSession(true):若存在会话则返回该会话,否则新建一个会话. request.getSession(false):若存在会话则返回该会话,否则返回NULL

  7. Django简单快速实现PUT、DELETE方法

    使用django的小伙伴们应该都知道我们是无法开心的处理PUT跟DELETE的 $.ajax({ url: 'XXX', type: 'PUT', dataType: 'json', data: { ...

  8. CSharp读取配置文件的类(简单实现)

    Reinventing the wheel 系列 CSharp 读取配置文件的类 简单实现(注意没有写) 本人对CS 不是很熟,库也不熟,所以到网上找个实现,并自己添加了点异常.如果只是读取信息,足够 ...

  9. 我对sobel算子的理解

    转自:http://blog.csdn.net/yanmy2012/article/details/8110316 索贝尔算子(Sobeloperator)主要用作边缘检测,在技术上,它是一离散性差分 ...

  10. Crypto Challenge Set 1解题报告

    1.Convert hex to base64 题意:给出一个hex编码过的字符串,将它进行base64加密 解题关键:直接利用base64库函数实现 import base64 str1=" ...