Atcoder D - Knapsack 1 (背包)
D - Knapsack 1
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≤1051≤W≤105
- 1≤wi≤W1≤wi≤W
- 1≤vi≤1091≤vi≤109
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
5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
Sample Output 2 Copy
5000000000
The answer may not fit into a 32-bit integer type.
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_d
题意:有N个团队,每一个团队有w[i]个人,并且有v[i]的贡献值。
现在你有一个W的容量的公司,你需要从这n个团队中挑选出几个(每一个团队只能挑选一次),使之团队人数的总和小于W,并且贡献值的总和尽可能的大。
思路:裸的普通的背包问题。
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#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 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 ***/
int n,m;
int w[maxn];
ll v[maxn];
ll dp[maxn];
int main()
{
gbtb;
cin>>n>>m;
repd(i,,n)
{
cin>>w[i]>>v[i];
}
repd(i,,n)
{
for(int j=m;j>=w[i];j--)
{
dp[j]=max(dp[j],1ll*dp[j-w[i]]+v[i]);
}
}
cout<<dp[m];
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 D - Knapsack 1 (背包)的更多相关文章
- Atcoder E - Knapsack 2 (01背包进阶版 ex )
E - Knapsack 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...
- 牛客多校第九场 D Knapsack Cryptosystem 背包
题意: 给你32个物品,给定一个容积,让你恰好把这个背包装满,求出装满的方案 题解: 暴力计算的话,复杂度$2^{32}$肯定会炸,考虑一种类似bsgs的算法,先用$2^{16}$的时间遍历前一半物品 ...
- 回溯算法_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50314839 本文出自:[梁敬明的博客] 1.回溯算法 回溯算法也叫试探法,通俗的将就 ...
- 贪心算法_01背包问题_Java实现
原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...
- Atcoder regular Contest 073(D - Simple Knapsack)
Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...
- 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背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- 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 ...
随机推荐
- mysqldump与innobackupex备份过程你知多少
mysqldump与innobackupex备份过程你知多少 测试库表创建(这里在同一个库下创建两个表,一个表为innodb引擎,一个为myisam引擎) root@localhost : (none ...
- pandas中DataFrame对象to_csv()方法中的encoding参数
当使用pd.read_csv()方法读取csv格式文件的时候,常常会因为csv文件中带有中文字符而产生字符编码错误,造成读取文件错误,在这个时候,我们可以尝试将pd.read_csv()函数的enco ...
- C# 响应微信发送的Token验证,文字、图文自动回复、请求客服对话.....
代码如下,有需要的可以参考: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- luogu P5151 HKE与他的小朋友
嘟嘟嘟 看到\(i\)变成了\(A_i\),我突然想起了置换这个东西.于是马上到网上学了一遍轮换乘法. 手模后发现轮换乘法满足结合律,但不满足交换律. 于是就可以快速幂啦. 需要注意的是每一次相乘是\ ...
- LeetCode - 排列相关题目
1.获取全排列 https://leetcode.com/problems/permutations/submissions/ 按字典序输出: 这里用的是vector<int>,不是引用. ...
- day16 Python 函数嵌套函数和作用域
#函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系 name = 'alex' def foo(): name='linhaifeng' def bar(): #name='wu ...
- Linux下 XordDos(BillGates)木马查杀记录
最近朋友的一台服务器突然网络异常,cpu占用率暴表,登录上去一查,cpu占用300% 左右,流量异常,经过看查进程,获取信息最终确认为中了dos木马,经过几天的研究,基本上已经清除,以下是清理记录. ...
- python编程入门之简介
引用百度百科: Python是一种面向对象.直译式计算机程序设计语言,由荷兰人Guido van Rossum发明于1989年,1991年发行第一个公开发行版.它常被昵称为胶水语言,它能够很轻松的把用 ...
- 理解socket.io(一)---相关的API
理解socket.io(一)---相关的API 1. 什么是Socket.IO?Socket.IO是node.js的一个模块,它用于浏览器与服务端之间实时通信.它提供了服务器和客户端的组件,只需一个模 ...
- 理解 DocumentFragment
理解 DocumentFragment 含义:创建文档片段,它继承了Node的所有方法,对DOM操作性能非常好.创建文档片段 如下方法: var frag = document.createDocum ...