传送门

题意

给出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. Solidworks如何制作动画2

    切换到Motion Study,然后定位到任意一帧,然后就可以摆弄当前装配体到新的位置和姿态,然后此时的时间和姿态就被记录下来了.以此类推可以多做几帧. 动画做好之后,点击播放可以预览.如果要保存,先 ...

  2. css设置背景图片自适应

      CreateTime--2017年12月25日16:36:07 Author:Marydon 控制背景图片100%自适应填充布局 /* 控制背景图片100%自适应填充布局 */ body{ bac ...

  3. iOS知识点全梳理-b

    感谢分享 原文链接:http://www.jianshu.com/p/5d2163640e26 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会 ...

  4. HDU 1085 Holding Bin-Laden Captive!(母函数,或者找规律)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. TinyXML学习:TiXmlBase类

    TiXmlBase: 作为整个TinyXML模型的基类,除了可以提供一些实用功能外,它几乎没有什么作用 TiXmlBase的友元类: friend class TiXmlNode; friend cl ...

  6. linux中select网络通信

    //ser.cpp #include <iostream> #include <fcntl.h> #include <sys/socket.h> #include ...

  7. java乱炖

    --------------------------------------------------------- ArrayList<String> arrayList = new Ar ...

  8. Appium基于安卓的各种FindElement的控件定位

    转自:http://www.2cto.com/kf/201410/340345.html 1. findElementByName 1.1 示例 ? 1 2 el = driver.findEleme ...

  9. solr单机多实例部署文件锁冲突解决的方法

    给出一个有问题的单机多tomcat实例引用同一个solr实例部署图. 这样的部署必定造成一个问题.启动第二个tomcat实例时,一定会报索引目录文件锁已经被占用. 最初的解决的方法是.有多少个tomc ...

  10. 基于mqtt协议实现手机位置跟踪

    Mqtt协议是物联网领域的一个标准协议,具有轻巧,对设备,带宽要求低,可靠稳定的特点,适合用来实现手机定位跟踪功能. 目前我初步搭建起来了整个可运行的框架,大致为如下思路:1.手机端通过位置服务,获取 ...