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],高度都是一样,把一些书竖着放,然后一些书横着放在同一层,就像下图那样放: 问你把所有的书放好之后竖着的书的总厚度是多少? 题解 ...
随机推荐
- disposition
该文为堕落的天使不再来原创.欢迎转载. 在尽心web开发时,可能遇到以下几种需求:(disposition配置) 希望某类或者某已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够 ...
- VC非法内存值的判断
0xcdcdcdcd - Created but not initialised0xdddddddd - Deleted0xfeeefeee - Freed memory set by NT's he ...
- Installing Intellij IDEA sublime-text-2 on Ubuntu
he installation on Linux is traditionally more complicated. I wonder why people complain about the l ...
- hadoop namenpde机制
1:通过查看hadoop的临时目录,看到有fsimage和edit文件 2:fsimage时namenode的元数据内存镜像文件,edit文件时hadoop客户端的操作记录, edit_inproce ...
- linux下wps,系统缺失字体:wingdings、wingdings 2、wingdings3
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAArcAAACdCAIAAAAhV8dZAAAgAElEQVR4nOzdd1wT9/8H8OvXfjvtt8
- WeedFS0.6.8-引用库列表
WeedFS 0.68新增了对cassandra数据库存储的支持及JSON Web Token(JWTs)安全的支持. github.com/gocql/gocql //filer/cassandra ...
- Codeigniter
最近准备接手改进一个别人用Codeigniter写的项目,虽然之前也有用过CI,但是是完全按着自己的意思写的,没按CI的一些套路.用在公众的项目,最好还是按框架规范来,所以还是总结一下,免得以后别人再 ...
- HDOJ(1115)多边形重心
Lifting the Stone http://acm.hdu.edu.cn/showproblem.php?pid=1115 题目描述:输入n个顶点(整数),求它们围成的多边形的重心. 算法:以一 ...
- git 忽视大小写
git config core.ignorecase false http://stackoverflow.com/questions/3011625/git-mv-and-only-change-c ...
- C#实现控制Windows系统关机、重启和注销的方法:
shutdown命令的参数: shutdown.exe -s:关机shutdown.exe -r:关机并重启shutdown.exe -l:注销当前用户 shutdown.exe -s -t 时间:设 ...