E - Knapsack 2


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

There are NN items, numbered 1,2,…,N1,2,…,N. For each ii (1≤i≤N1≤i≤N), Item ii has a weight of wiwi and a value of vivi.

Taro has decided to choose some of the NN items and carry them home in a knapsack. The capacity of the knapsack is WW, which means that the sum of the weights of items taken must be at most WW.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

  • All values in input are integers.
  • 1≤N≤1001≤N≤100
  • 1≤W≤1091≤W≤109
  • 1≤wi≤W1≤wi≤W
  • 1≤vi≤1031≤vi≤103

Input

Input is given from Standard Input in the following format:

NN WW
w1w1 v1v1
w2w2 v2v2
::
wNwN vNvN

Output

Print the maximum possible sum of the values of items that Taro takes home.


Sample Input 1 Copy

Copy
3 8
3 30
4 50
5 60

Sample Output 1 Copy

Copy
90

Items 11 and 33 should be taken. Then, the sum of the weights is 3+5=83+5=8, and the sum of the values is 30+60=9030+60=90.


Sample Input 2 Copy

Copy
1 1000000000
1000000000 10

Sample Output 2 Copy

Copy
10

Sample Input 3 Copy

Copy
6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3 Copy

Copy
17

Items 2,42,4 and 55 should be taken. Then, the sum of the weights is 5+6+3=145+6+3=14, and the sum of the values is 6+6+5=176+6+5=17.

题目链接:https://atcoder.jp/contests/dp/tasks/dp_e

思路:体积虽然很huge,但价值很小。把最大化价值,转成最小化体积,就还是原来的01背包了。(RUSH_D_CAT大佬指点的)

很不错的一个背包优化的思路,细节见我的代码。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll dp[maxn];
ll n,W;
ll v[maxn];
ll w[maxn];
int main()
{
memset(dp,0x3f,sizeof(dp));
gbtb;
cin>>n>>W;
repd(i,,n)
{
cin>>w[i]>>v[i];
}
ll ans=0ll;
dp[]=;
repd(i,,n)
{
for(int j=1e5;j>=v[i];--j)
{
{
dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
if(dp[j]<=W)
ans=max(ans,(ll)(j));
}
}
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Atcoder E - Knapsack 2 (01背包进阶版 ex )的更多相关文章

  1. Educational DP Contest E - Knapsack 2 (01背包进阶版)

    题意:有\(n\)个物品,第\(i\)个物品价值\(v_{i}\),体积为\(w_{i}\),你有容量为\(W\)的背包,求能放物品的最大价值. 题解:经典01背包,但是物品的最大体积给到了\(10^ ...

  2. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  3. FZU - 2214 Knapsack problem 01背包逆思维

    Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...

  4. 2018.08.10 atcoder Median Sum(01背包)

    传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...

  5. Atcoder D - Knapsack 1 (背包)

    D - Knapsack 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...

  6. HDU-1421-搬寝室(01背包改编版)

    搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太 ...

  7. Atcoder Beginner Contest145E(01背包记录路径)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[3007],b[3007];int ...

  8. FOJProblem 2214 Knapsack problem(01背包+变性思维)

    http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4    Submit: 6Time Limit: 3000 mSec    Memory Lim ...

  9. FZU 2214 ——Knapsack problem——————【01背包的超大背包】

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

随机推荐

  1. 数据库之mysql篇(2)—— mysql常识引入/用户授权

    常识引入 1.概念: 数据库:本质上是一个文件夹 1)查看本机所有数据库:show databases; 结束符:分号[:],一切数据行的结尾都以分号作为结束 2)创建数据库:create  数据库名 ...

  2. 预热一下吧《实现Redis消息队列》

    应用场景 为什么要用redis?二进制存储.java序列化传输.IO连接数高.连接频繁 一.序列化 这里编写了一个java序列化的工具,主要是将对象转化为byte数组,和根据byte数组反序列化成ja ...

  3. 在Lua中提示UnityEngine.dll的方法

    我的环境 安装最新的 EmmyLua-1.2.1及以上版本 IDEA 2017.1.2 及以上版本 关于EmmlyLua的介绍可查看我之前的文章:Lua代码提示和方法跳转 说明:本文方法摘自 Emmy ...

  4. 基于PHP的颜色生成器

    <?php  function randomColor()  {      $str = '#';      for($i = 0 ; $i < 6 ; $i++)     {      ...

  5. puppet使用 apache passsenger 作为前端 (centos)

    目录 1. 概要 2. nginx + passenger 配置 2.1. package 安装 2.2. 配置文件设置 2.3. rack 目录生成 概要 使用 nginx + passenger ...

  6. UDP Health Checks

    This chapter describes how to configure different types of health checks for UDP servers in a load-b ...

  7. 搭建golang学习环境,并用chrome headless获取网页内容

    想用go练练手(我是win7系统,已从https://studygolang.com/dl 下载了go安装包并安装,比较简单,不详述. 但作为边民,没法go get ,又不敢用梯子,幸亏有爱心大牛们的 ...

  8. [转自机器之心] 刚入校门的PhD们还可以抢救一下(读研读博指南)

    本文作者 Lucy A. Taylor 最近博士毕业,取得了牛津大学跨学科生物科学博士学位. 读博是件难事,一路上可能会遇到很多挫折.失败.崩溃时刻.Lucy 多么希望在开始读博时就能收到一些有益的建 ...

  9. CROI R1

    $CROI$ $R1$ 今天参加了一场比赛,什么比赛呢?CROI. CROI是什么呢? $Challestend$ $Rehtorbegnaro$ $OI$.总的来说就是我们机房的一些神仙出的题啦. ...

  10. DataGrid获取单元格的值

    string str = (dataGrid.Columns[0].GetCellContent(dataGrid.Items[0]) as TextBlock).Text;