http://codeforces.com/problemset/problem/19/B

对于每个物品,能偷多ti个,那么先让ti + 1, 表示选了这个东西后,其实就是选了ti + 1个了。那么只需要选出>=n个即可。

一开始的时候想不到ti + 1,一直不知道能多选ti个后,本来是选了多少个。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e3 + ;
int w[maxn];
int val[maxn];
LL dp[ * + ];
void work() {
int n;
cin >> n;
int sum = ;
for (int i = ; i <= n; ++i) {
cin >> w[i] >> val[i];
w[i]++;
w[i] = min(w[i], );
sum += w[i];
}
for (int i = ; i <= * - ; ++i) {
dp[i] = 1e17L;
}
dp[] = ;
sum = min(, sum);
for (int i = ; i <= n; ++i) {
for (int j = sum; j >= w[i]; --j) {
dp[j] = min(dp[j], dp[j - w[i]] + val[i]);
}
}
LL ans = 1e17L;
for (int i = n; i <= sum; ++i) {
ans = min(ans, dp[i]);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

写了一个超时的二维费用背包

dp[i][j]表示前n个数选出i个,有j个可以免费拿的情况。

直接超时。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e3 + ;
LL dp[maxn][maxn];
int ti[maxn];
int cost[maxn]; void work() {
int n;
cin >> n;
int sum = ;
for (int i = ; i <= n; ++i) {
cin >> ti[i] >> cost[i];
sum += ti[i];
}
for (int i = ; i <= + ; ++i) {
for (int j = ; j <= + ; ++j) {
dp[i][j] = 1e17L;
}
}
dp[][] = ;
sum = min(, sum);
for (int i = ; i <= n; ++i) {
for (int j = i; j >= ; --j) {
for (int k = sum; k >= ti[i]; --k) {
dp[j][k] = min(dp[j][k], dp[j - ][k - ti[i]] + cost[i]);
}
}
}
LL ans = 1e17L;
for (int i = ; i <= n; ++i) {
for (int j = sum; j >= ; --j) {
dp[i][j] = min(dp[i][j], dp[i][j + ]);
}
}
for (int i = ; i <= n; ++i) {
ans = min(ans, dp[i][n - i]);
}
cout << ans << endl;
// cout << dp[3][5] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}

B. Checkout Assistant 01背包变形的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

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

  2. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  3. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  4. 【01背包变形】Robberies HDU 2955

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...

  5. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  6. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  7. POJ 3211 Washing Cloths(01背包变形)

    Q: 01背包最后返回什么 dp[v], v 是多少? A: 普通01背包需要遍历, 从大到小. 但此题因为物品的总重量必定大于背包容量, 所以直接返回 dp[V] 即可 update 2014年3月 ...

  8. HDU 2955 Robberies(01背包变形)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

随机推荐

  1. IRC常用命令

    From: http://www.blogjava.net/ivanwan/archive/2006/02/17/31296.html IRC命令可以实现一些特殊的功能.目前,亿言堂支持14条IRC命 ...

  2. 微信小程序--摸索之旅

    首先 奉上腾讯官方文档 方便参考:https://mp.weixin.qq.com/debug/wxadoc/design/index.html  个人认为没说啥特别有用的信息(可能是我看的太粗糙了) ...

  3. iOS静态分析举例

    XCode-> Product -> Analyze 即可进行iOS静态代码分析.静态分析能发现的问题包括以下几种类型: 1.逻辑错误:访问空指针或未初始化的变量等: 2.内存管理错误:如 ...

  4. 用ionic快速开发hybird App(已附源码,在下面+总结见解)

    1.ionic简介 ionic 是用于敏捷开发APP的解决方案.核心思路是:利用成熟的前端开发技术,来写UI和业务逻辑.也就是说,就是一个H5网站,这个区别于react-native,native.即 ...

  5. WS+MQ+WCF+EF(Code First)

    前言 有段时间没有更新博文了,一直在忙工作很少有时间静下心来继续研究点东西,说来也惭愧,归咎原因最主要的还是因为懒惰.空想也是不管用的,有时候很多想法被扼杀到了摇篮里,还没开始做就放弃了,这是多数人会 ...

  6. centos下ssh无密码验证

    #安装openssh-clients,rsync等#1.修改所有master和slave服务器的sshd_config,后面增加UseDNS noClientAliveInterval 30RSAAu ...

  7. ssh访问控制,多次失败登录即封掉IP,防止暴力破解

    ssh访问控制,多次失败登录即封掉IP,防止暴力破解 一.系统:Centos6.3 64位 二.方法:读取/var/log/secure,查找关键字 Failed,例如(注:文中的IP地址特意做了删减 ...

  8. FileInputStream、FileReader、FileInputStream、FileWriter使用小结

    本文是基于Linux环境运行,读者阅读前需要具备一定Linux知识 InputStream包含如下三个方法: int read():从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转化为i ...

  9. asp.net core获取自定义json的配置内容

    首先在主目录下建立:Iyibank.Web.json文件 里边的内容如下: { "ConnectionStrings": { "RedisCache": &qu ...

  10. requirejs的使用

    requirejs的优点: 1.防止在js的加载过程中,阻止页面的渲染: 2.可以引入多个js文件: 3.可以写出重复使用的js模块: 4.有效的防止命名的冲突,通过将变量分装在模块中的方式实现: r ...