Atcoder E - Knapsack 2 (01背包进阶版 ex )
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
3 8
3 30
4 50
5 60
Sample Output 1 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
1 1000000000
1000000000 10
Sample Output 2 Copy
10
Sample Input 3 Copy
6 15
6 5
5 6
6 4
6 6
3 5
7 2
Sample Output 3 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 )的更多相关文章
- Educational DP Contest E - Knapsack 2 (01背包进阶版)
题意:有\(n\)个物品,第\(i\)个物品价值\(v_{i}\),体积为\(w_{i}\),你有容量为\(W\)的背包,求能放物品的最大价值. 题解:经典01背包,但是物品的最大体积给到了\(10^ ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- 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 ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...
- Atcoder D - Knapsack 1 (背包)
D - Knapsack 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...
- HDU-1421-搬寝室(01背包改编版)
搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太 ...
- Atcoder Beginner Contest145E(01背包记录路径)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[3007],b[3007];int ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
随机推荐
- timeout 命令
命令简介 运行指定的命令,如果在指定时间后仍在运行,则杀死该进程.用来控制程序运行的时间. 使用方法 1 2 3 timeout [选项] 数字[后缀] 命令 [参数]... 后缀 s 代表秒(默认值 ...
- Shell脚本常用模板
作为一个运维人员编写Shell脚本是很平常的,一个格式好的脚本不仅赏心悦目,后期自己和别人也易于维护. 下面的脚本就是我自己的shell编写格式,如下: [root@mini05 -]# cat te ...
- 解决ubuntu下,QQ重启后出现个人文件夹已被占用的问题
首先,是wine QQ的安转教程:Wine安装最新版QQ(8.9.2)的简单教程 - Powered by Discuz! 里面作者也提到了关于重启后出现个人文件夹被占用的情况. 如下: 这里,如果不 ...
- ID 生成器
using System; using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Thr ...
- Codeforces Round #542 C. Connect 搜索
C. Connect time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Jenkins+Ansible+Gitlab自动化部署三剑客-gitlab本地搭建
实际操作 准备linux初始环境 关闭防火墙 systemctl stop firewalld 开机自己关闭 systemctl disable firewalld 设置安全配置 为关闭 vim /e ...
- UVA1620-Lazy Susan(思维+逆序对)
Problem UVA1620-Lazy Susan Accept: 81 Submit: 375Time Limit: 3000 mSec Problem Description There ar ...
- UVA120-Stacks of Flapjacks(思维)
Problem UVA120-Stacks of Flapjacks Accept: 9232 Submit: 38455Time Limit: 10000 mSec Problem Descrip ...
- tensorflow中的batch_normalization实现
tensorflow中实现batch_normalization的函数主要有两个: 1)tf.nn.moments 2)tf.nn.batch_normalization tf.nn.moments主 ...
- 【转】Android多进程总结一:生成多进程(android:process属性)
前言 正常情况下,一个apk启动后只会运行在一个进程中,其进程名为apk的包名,所有的组件都会在这个进程中运行,以下为DDMS的进程截屏: com.biyou.multiprocess为进程名,也是a ...