B. Shaass and Bookshelf DP
http://codeforces.com/contest/294/problem/B
据说是贪心,我用了一个复杂度是2e8的dp水过去了。
其实这题就是给你n个数,每个数有两个权值,分成两组,使得第一个权值之和,和第二个权值之和的最大值最小。
那么直接设dp[i][j][k][h]表示前i个数中,选了j个,第一个权值的和是k,第二个权值是h,是否可能。
这里是一定要选出n个的,就是n个数都必须使用,才能滚动数组。(把第二维滚动)
如果是从n个数中选出k个,那么就不能滚动了。
那么第一维是01背包直接省略,j那里可以滚动数组。
总复杂度2e8
然后题解是贪心......
还有要注意,k要大于h,就是底下的书要长于上面的书。
#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>
struct node {
int ti, wi;
}a[];
int dp[][ + ][ + ];
void work() {
int n;
cin >> n;
int DFN = ;
int sumti = , sumwi = ;
for (int i = ; i <= n; ++i) {
cin >> a[i].ti >> a[i].wi;
sumti += a[i].ti;
sumwi += a[i].wi;
}
if (n == ) {
cout << min(a[].ti, a[].wi) << endl;
return;
}
// sort(a + 1, a + 1 + n);
dp[][][] = DFN;
int now = ;
for (int i = ; i <= n; ++i) {
now = !now;
++DFN;
for (int j = sumti; j >= ; --j) {
for (int k = sumwi; k >= ; --k) {
if (j >= a[i].ti && dp[!now][j - a[i].ti][k] == DFN - ) {
dp[now][j][k] = DFN;
}
if (k >= a[i].wi && dp[!now][j][k - a[i].wi] == DFN - ) {
dp[now][j][k] = DFN;
}
}
}
}
int ans = inf;
for (int i = ; i <= sumti; ++i) {
for (int j = ; j <= i; ++j) {
if (dp[now][i][j] == DFN) {
ans = min(ans, max(i, j));
}
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
B. Shaass and Bookshelf DP的更多相关文章
- Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
题目链接:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 secon ...
- CodeForces 294B Shaass and Bookshelf 【规律 & 模拟】或【Dp】
这道题目的意思就是排两排书,下面这排只能竖着放,上面这排可以平着放,使得宽度最小 根据题意可以得出一个结论,放上这排书的Width 肯定会遵照从小到大的顺序放上去的 Because the total ...
- Codeforces 294B Shaass and Bookshelf:dp
题目链接:http://codeforces.com/problemset/problem/294/B 题意: 有n本书,每本书的厚度为t[i],宽度为w[i] (1<=t[i]<=2, ...
- Codeforces K. Shaass and Bookshelf(动态规划三元组贪心)
题目描述: B. Shaass and Bookshetime limit per test 2 secondsmemory limit per test 256 megabytesinput ...
- [CF294B]Shaass and Bookshelf
问题描述 Shaass拥有n本书.他想为他的所有书制作一个书架,并想让书架的长宽尽量小.第i本书的厚度是t[i],且这本书的纸张宽度是w[i].书的厚度是1或2,所有书都有同样的高度(即书架的高是均匀 ...
- Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensi ...
- Codeforces 294B Shaass and Bookshelf(记忆化搜索)
题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...
- [Luogu1848][USACO12OPEN]书架Bookshelf DP+set+决策单调性
题目链接:https://www.luogu.org/problem/show?pid=1848 题目要求书必须按顺序放,其实就是要求是连续的一段.于是就有DP方程$$f[i]=min\{f[j]+m ...
- Codeforces294B - Shaass and Bookshelf(贪心)
题目大意 给你N本书,每本书由一个厚度t[i](1或者2),宽度w[i],高度都是一样,把一些书竖着放,然后一些书横着放在同一层,就像下图那样放: 问你把所有的书放好之后竖着的书的总厚度是多少? 题解 ...
随机推荐
- (2016 年) githup 博客地址 : https://github.com/JMWY/MyBlog
githup 博客地址 : https://github.com/JMWY/MyBlog
- Django RedirectView
RedirectView作用是重定向一个指定,给定的Url.这个给定的Url可能包含有字典风格的字符串,因为关键字(词)会被改变,所以从这个Url中捕获的参数可能也会被修改,例如,Url中的“%”应该 ...
- Druid安装-单机
单机版安装 下载安装包http://static.druid.io/artifacts/releases/druid-0.9.1.1-bin.tar.gz 安装 解压缩 安装zookeeper cu ...
- delphi.指针.PChar
此文是delphi.指针.应用姊妹篇,想细化一下PChar应用,所以有了此文. 注意: 1:此文讲的是PChar与字符串相关操作,其它方法暂不多讲. 2:由于D分开Ansi/Unicode的两种完全不 ...
- 【转载】快速收索并更新sid 方法
利用Google的搜索功能,可以获得不少SAS各个版本的SID号,试过之后你会异常惊喜.1.打开谷歌: http://google.com.hk2.输入或复制这个段文字:"SID_heade ...
- NOIP模拟赛-征兵
一个国王,他拥有一个国家.最近他因为国库里钱太多了,要征集一只部队要保卫国家.他选定了N个女兵和M个男兵,但事实上每征集一个兵他就要花10000RMB,即使国库里钱再多也伤不起啊.他发现,某男兵和某女 ...
- jquery文件上传控件 Uploadify
(转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...
- nvm
nvm install stable #安装最新稳定版 node,现在是 5.0.0 nvm install 4.2.2 #安装 4.2.2 版本 nvm install 0.12.7 #安装 0.1 ...
- WCF ajax跨域配置
webconfig必须配置 binding="webHttpBinding" <service name="Hezi.MsgService.Send"&g ...
- NoSQL学习——MongoDB
MongoDB作为一款文档数据库,支持分片存储,scale-out,集群自动切换,下面将粗略的配置步骤总结如下: 几个重要概念: 数据库:集合--记录--游标(查询时标记序号) sharding分片: ...