Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
题目链接:http://codeforces.com/contest/294/problem/B
1 second
256 megabytes
standard input
standard output
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible.
The thickness of the i-th book is ti and
its pages' width is equal to wi.
The thickness of each book is either 1 or 2.
All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more
than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.
The first line of the input contains an integer n, (1 ≤ n ≤ 100).
Each of the next n lines contains two integers ti and wi denoting
the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
5
1 12
1 3
2 15
2 5
2 1
5
3
1 10
2 1
2 4
3
题解:
数据范围很小,所以可以直接开三维数组。
1.dp[i][j][k]表示:第i本书,下面为j, 上面为k的状态是否存在。
2.对于每一个已经存在的状态,再去判断当前的书是否可以放上去。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 100+10; int n;
int w[maxn], t[maxn];
int dp[maxn][maxn<<1][maxn<<1]; int main()
{
scanf("%d",&n);
int sum = 0;
for(int i = 1; i<=n; i++)
scanf("%d%d", &t[i], &w[i]), sum += t[i]; dp[0][sum][0] = 1; //初始化全部放在下面
for(int i = 1; i<=n; i++)
for(int j = sum; j>=0; j--)
for(int k = 0; k<=j; k++)
{
if(!dp[i-1][j][k]) continue; //如果放在下面的状态不存在,则直接退出 dp[i][j][k] = 1; //留在下面
if(j-t[i]>=k+w[i]) //放上去
dp[i][j-t[i]][k+w[i]] = 1;
} int ans = INF;
for(int j = sum; j>=0; j--)
for(int k = 0; k<=j; k++)
if(dp[n][j][k])
ans = min(ans,j);
cout<< ans <<endl;
return 0;
}
或者用记忆化搜索写:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 100+10; int n;
int w[maxn], t[maxn];
int dp[maxn][maxn<<1][maxn<<1]; int dfs(int pos, int thi, int wid)
{
if(pos==n+1) return thi;
if(dp[pos][thi][wid]!=-1) return dp[pos][thi][wid]; if(thi-t[pos]>=w[pos]+wid)
return dp[pos][thi][wid] = min( dfs(pos+1, thi-t[pos], w[pos]+wid), dfs(pos+1, thi, wid) );
else
return dp[pos][thi][wid] = dfs(pos+1, thi, wid);
} int main()
{
scanf("%d",&n);
int sum = 0;
for(int i = 1; i<=n; i++)
scanf("%d%d", &t[i], &w[i]), sum += t[i]; memset(dp,-1,sizeof(dp));
cout<< dfs(1, sum, 0) <<endl;
return 0;
}
Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP的更多相关文章
- 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 Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #178 (Div. 2)
A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #265 (Div. 1) C. Substitutes in Number dp
题目链接: http://codeforces.com/contest/464/problem/C J. Substitutes in Number time limit per test 1 sec ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块
题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...
随机推荐
- 洛谷——P1187 3D模型
P1187 3D模型 题目描述 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上 ...
- Oracle服务扫描工具Oscanner
Oracle服务扫描工具Oscanner Oracle是甲骨文公司推出的关系型数据库,适用于中大规模数据存储,如大型企业.电信.银行等行业.Kali Linux集成了Oracle服务扫描专向工具O ...
- window下安装tensowflow
tensorflow0.12版本支持windows,但是需要python3.5.x.而我的本机只安装了Anaconda2.7版本.因此我们可以在Anaconda中新增Python3.5的环境. 首先, ...
- C++ 面试问题
一面 (1) 多态性都有哪些?(静态和动态,然后分别叙述了一下虚函数和函数重载) (2) 动态绑定怎么实现?(就是问了一下基类与派生类指针和引用的转换问题) (3) 类型转换有哪些?(四种类型转换,分 ...
- php-fpm.conf配置说明(重点要改动和优化的地方)
<?xml version="1.0" ?> <configuration> All relative paths in this config are r ...
- 使用Cout输出String和CString对象
CString和string都是一个类,不同的是CString主要用于MFC或者是ATL编程中,而string则多用于Windows控制台编程中 在实际编程过程中,我们经常用到string或者是CSt ...
- VUE高仿饿了么app开发思维导图
来自互联网 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing
- 江湖问题研究-- intent传递有没有限制大小,是多少?
出门一步,便是江湖.江湖上有很多流言. 比方这条: intent传递是有限制大小的,详细在40KB左右. 当然也有传言说是1M左右. 数百头母驴为何半夜慘叫? 小卖部安全套为何屡遭黑手? 女生宿舍内裤 ...
- D堆的实现
实现上一篇博客(http://blog.csdn.net/buleriver/article/details/38469977)说的D堆.假设把mD设置成2.D堆就退化成二叉堆,也就是说.二叉堆是D堆 ...
- RabbitMQ的工作模式
简单模式: # #########################基于简单模式的 生产者 ######################### #!/usr/bin/env python import ...