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. 【IL】IL生成exe的方法

    1.将ilasm.exe和fusion.dll用everything找出来,然后复制到system32文件夹 2.执行:ilasm /res:code.res code.il /out:code.ex ...

  2. 对restful的理解

    最近在学习web api,从而涉及到了restful风格,我的理解是restful风格,每个链接都会对资源进行相应的操作(CRUD),如果一个链接不包含资源,则可能不符合restful风格,借此想请教 ...

  3. Linux学习二:Makefile基础

    文首感谢http://www.chinaunix.net 作者:gunguymadman的分享 makefile关系到了整个工程的编译规则.一个工程中的源文件不计数,其按类型.功能.模块分别放在若干个 ...

  4. items2 配色

    cat ~/.bash_profile #enables colorin the terminal bash shell exportexport CLICOLOR=1 #sets up thecol ...

  5. eclipse内嵌jetty(run-jetty-run插件) 配置jndi数据源

    运行环境 java 6,eclipse juno,ssh(spring,hibernate,springmvc ) 1.离线安装 下载地址:http://pan.baidu.com/s/1qX67wO ...

  6. express 快速教程

    阅读 express 官方文档的记录. hello world example var express = require('express') var app = express() app.get ...

  7. WIn7系统下 打开.exe程序出现已停止工作关闭程序之解决办法

    新装WIN7系统出现  .NET组建没有安装  可到官网下载安装 NETFx4.0 运行MVB 上位机SIM.EXE出现应用程序已停止工作问题 解决办法: 需关闭WIN7 DEP  如下 开始-运行( ...

  8. rabbitMQ学习(四)

    按照routing key接收信息 发送端: public class EmitLogDirect { private static final String EXCHANGE_NAME = &quo ...

  9. svn本地客户端和eclipse插件对应不上解决

    如果你是SVN来管理代码,如果你是用eclipse搞开发,如果你知道eclipse有SVN的插件,那么你一定想用它来直接管理你的代码.但是事情往往非人所愿.当我们用SVN客户端把代码更新到本地,并导入 ...

  10. 实现iframe 全屏显示

    componentDidMount(){var elem = document.getElementById('iframe');; var elem = document.getElementByI ...