传送门

题意

给出n个体积为wi,价值为ci的物品,现在有一个m大的背包

问如何装使得最后背包内的物品价值最大,输出价值

分析

一般的思路是01背包,但n*v不可做

题解的思路

We can iterate on the number of 3-elements we will take (in this editorial k-element is a souvenir with weight k). When fixing the number of 3-elements (let it be x), we want to know the best possible answer for the weight m - 3x, while taking into account only 1-elements and 2-elements.

To answer these queries, we can precalculate the values dp[w] — triples (cost, cnt1, cnt2), where cost is the best possible answer for the weight w, and cnt1 and cnt2 is the number of 1-elements and 2-elements we are taking to get this answer. Of course, dp[0] = (0, 0, 0), and we can update dp[i + 1] and dp[i + 2] using value of dp[i]. After precalculating dp[w] for each possible w we can iterate on the number of 3-elements.

这道题我还没有真正理解,留坑

trick

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std; typedef long long ll; const int Maxm = 300015;
const int Maxw = 3; int n, m;
ll best[Maxm];
vector <int> seq[Maxw]; ll Solve()
{
int i = 0, j = 0;
ll cur = 0, w = 0;
ll res = best[m];
if (i < seq[0].size() && 1 <= m) res = max(res, seq[0][i] + best[m - 1]);
while (i + 2 <= seq[0].size() || j + 1 <= seq[1].size()) {
if (i + 2 <= seq[0].size() && (j + 1 > seq[1].size() || seq[0][i] + seq[0][i + 1] > seq[1][j])) {
cur += seq[0][i] + seq[0][i + 1]; w += 2; i += 2;
} else {
cur += seq[1][j]; w += 2; j++;
}
if (w <= m) res = max(res, cur + best[m - w]);
if (w + 1 <= m) {
if (i < seq[0].size()) res = max(res, cur + ll(seq[0][i]) + best[m - w - 1]);
if (i > 0 && j < seq[1].size()) res = max(res, cur + ll(seq[1][j]) - ll(seq[0][i - 1]) + best[m - w - 1]);
}
}
return res;
} int main()
{
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
int w, c; scanf("%d %d", &w, &c);
seq[w - 1].push_back(c);
}
for (int i = 0; i < Maxw; i++)
sort(seq[i].rbegin(), seq[i].rend());
ll cur = 0;
for (int i = 0; i < seq[2].size(); i++) {
cur += seq[2][i];
best[3 * (i + 1)] = cur;
}
for (int i = 0; i + 1 <= m; i++)
best[i + 1] = max(best[i + 1], best[i]);
printf("%I64d\n", Solve());
return 0;
}

Educational Codeforces Round 21E selling souvenirs (dp)的更多相关文章

  1. Educational Codeforces Round 63-D(基础DP)

    题目链接:https://codeforces.com/contest/1155/problem/D 题意:给定n个数,可以选择一段连续子段将其乘x,也可以不操作,求最大连续子段和. 思路:比赛时觉得 ...

  2. Educational Codeforces Round 62 E 局部dp + 定义状态取消后效性

    https://codeforces.com/contest/1140/problem/E 局部dp + 定义状态取消后效性 题意 给你一个某些位置可以改变的字符串,假如字符串存在回文子串,那么这个字 ...

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

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

  4. Educational Codeforces Round 52F(树形DP,VECTOR)

    #include<bits/stdc++.h>using namespace std;int n,k;vector<int>son[1000007];int dp[100000 ...

  5. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  6. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  7. Educational Codeforces Round 21

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

  8. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  9. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

随机推荐

  1. JQUERY多选框,单选框,检查选中的值

    var str=""; $(":checkbox:checked").each(function(){ if($(this).attr("checke ...

  2. navicat-premium11.1.6 x64关键

    Navicat Premium version patch   11.2.16.0 >navicat.exe0000000001CECCBC:80->C60000000001CECCBD: ...

  3. POJ--2284--That Nice Euler Circuit【平面图欧拉公式】

    链接:id=2284">http://poj.org/problem?id=2284 题意:一个自己主动绘图的机器在纸上(无限大)绘图,笔尖从不离开纸,有n个指令,每一个指令是一个坐标 ...

  4. 初识glib(1)

    最近搞DLNA,发现download的源码有许多glib库的使用.于是在Ubuntu中安装了glib库,以及简单测试了一些glib库函数,以此增加对glib的了解. 概述:glib库是Linux平台下 ...

  5. iOS面试常见题

    1.耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13个人围坐一圈,从第一个人開始循环报数,数到三排除,最后剩下的人就是叛徒 int people[13] = {1,2,3, ...

  6. group by where having 联合使用

    having子句与where有相似之处但也有区别,都是设定条件的语句.在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先执行.而where子句在查询过程中执行优 ...

  7. LeetCode之16----3Sums Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  8. x$kccle视图深入剖析

       今天是2014-05-27,实在无聊顺便研究一下x$kccle的内容吧.例如以下所有是自己分析和实验结果,真实可靠.   1.怎样获得v$log的底层表?我们能够通过autotrace完毕查看如 ...

  9. i MySQL 查看约束,添加约束,删除约束

    查看表的字段信息:desc 表名; 查看表的所有信息:show create table 表名; 添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) ...

  10. Spark基本原理

    仅作<Spark快速大数据分析>学习笔记 定义:Spark是一个用来实现 快速 而 通用 的集群计算平台:(通用的大数据处理引擎:) 改进了原Hadoop MapReduce处理模型,体现 ...