传送门

题意

给出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;
}

最新文章

  1. 初识html5的localStorage本地存储
  2. javadoc
  3. SQL2008使用json.net实现XML与JSON互转
  4. Java代码常用写法总结
  5. Intellij IDEA 配置Subversion插件
  6. 初学Java9:学习Mybatis时报错:Parameter &#39;name&#39; not found. Available parameters are [1, 0, param1, param2]
  7. ORA-08189
  8. android136 360 拖拽
  9. [cocos2d] 谁摸了我一下----触摸事件处理
  10. 一般处理程序、ASP.NET核心知识(5)--转载
  11. Oracle EBS-SQL (BOM-15):检查多层BOM(含common BOM).sql
  12. linux下制作镜像文件
  13. 将正在运行的Proxmox EV5.x版本更新到最新版本
  14. OpenCV3 for python3 学习笔记1
  15. 2017年4月7日16:18:17 java8 常用记录
  16. angularjs学习第八天笔记(指令作用域研究)
  17. windows下rabbitmq-c编译(带openssl、无需MinGW)
  18. vue全家桶+Koa2开发笔记(7)--登陆注册功能
  19. PowerDesigner 打印错误
  20. redis集群节点宕机

热门文章

  1. C++与Java语法上的不同
  2. 转:Python yield 使用浅析 from IBM Developer
  3. 入手Arduino Yun,配合Blynk搞一波事情
  4. [LightOJ 1018]Brush (IV)[状压DP]
  5. JfreeChart折线图 CSDN-李鹏飞
  6. MySQL(6)--复制,docker容器中
  7. 命令行下Android应用开发
  8. Printf可变參数使用
  9. sanic官方文档解析之ssl,debug mode模式和test(测试)
  10. JVM的CPU资源占用过高问题的排查