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. OS X 系统,修改hosts文件后不生效的问题

    系统版本 OS X El Capitan,10.11.2 问题描述 业务需要,配置“nexus”为某个ip,如下图更改了 /etc/hosts 文件. 结果在ping的时候,请求并未发到指定ip上. ...

  2. .NET生成静态页面并分页

    因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序. 主要的原理就是替换模板里的特殊字符. 1.静态 ...

  3. Ubuntu 14.04下Django+MySQL安装部署全过程

    一.简要步骤.(Ubuntu14.04) Python安装 Django Mysql的安装与配置 记录一下我的部署过程,也方便一些有需要的童鞋,大神勿喷~ 二.Python的安装 由于博主使用的环境是 ...

  4. 在Mac上配置adb命令

    在Mac上配置adb命令 在Mac OS中使用adb命令时,应进行变量配置,步骤如下: 一.终端中输入 cd ~ 二.输入touch .bash_profile 回车 touch:如果没有,则创建文件 ...

  5. JS定义函数的两种方式:函数声明和函数表达式

    函数声明 关于函数声明的方式,它的一个重要的特性就是函数声明提升(function declaration hoisting),意思是在执行代码之前会先读取函数声明.这就意味着可以把函数声明放在调用它 ...

  6. 你还记得windows workflow foundation吗

    很多年前,windows workflow foundation还叫WWF,而直译过来的名称让很多人以为它就是用来开发工作流或者干脆就是审批流的. 博主当年还是个懵懂的少年,却也知道微软不会大力推一个 ...

  7. Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境搭建教程

    原文地址:http://www.osyunwei.com/archives/7378.html 搬运是为了自己找资料方便. 准备篇 一.环境说明: 操作系统:Windows Server 2012 R ...

  8. Hibernate温习(一)

    //从学校出来几个月了,一直用maximo没有使用到Hibernate,趁着周末的空闲时间重新开始学习Hibernate. Hibernate概念: Hibernate是数据库访问层的框架,对JDBC ...

  9. 美国安全公司HBGary——国家授命的黑客

         入侵电脑,窃听用户,假身份上网——美国安全公司HBGary是电脑防护和间谍软件的供应商.而其客户中就有美国的国家安全机构.现在,该公司被偷的电子邮件可以让我们对其数字化战争中的业务做一个初步 ...

  10. $(document).ready()方法和window.onload区别

    事件: javascript 和 HTML之间的交互式通过用户和浏览器操作页面时引发的事件来处理的.当文档或者它的某些元素发生某些变化和操作时,浏览器会自动生成一个事件:例如:当用户单击某个按钮时,也 ...