Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
来源:互联网 发布:官方刷机软件 编辑:程序博客网 时间:2024/06/05 20:26
题目链接:http://codeforces.com/contest/294/problem/B
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.
51 121 32 152 52 1
5
31 102 12 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
- Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf
- Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf 01背包
- codeforces 294B B. Shaass and Bookshelf(dp)
- codeforces 294B Shaass and Bookshelf (暴力dp)
- codeforces 294B Shaass and Bookshelf
- Codeforces Round #178 (Div. 2)---A. Shaass and Oskols
- Codeforces Round #178 (Div. 2) / 294A Shaass and Oskols(模拟)
- Codeforces Round #294 (Div.2) E Shaass the Great
- Codeforces Round #240 (Div. 1)---B.Mashmokh and ACM(dp)
- Codeforces Round #178 (Div. 2) A and B
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round #178 (Div. 2)——A,B
- Codeforces Round #272 (Div. 2) B Dreamoon and WiFi(简单DP)
- Codeforces Beta Round #61 (Div. 2) B. Petya and Countryside (DP)
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp,组合数学
- 【Codeforces Round #442 (Div. 2)】 B. Nikita and string (线性状态dp)
- 关于在nginx负载均衡下,项目共用log4j配置导致的日志混乱问题
- 小米5 Google服务框架安装
- 两重数组排序问题
- eclipse打jar包时也将依赖的jar包打到一起的解决办法
- 盘点各专业到古代都能干些啥
- Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP
- Linux下常用系统分析工具总结
- 将oracle的数据导入到mysql的四种方法
- 我理解的虚拟DOM
- Spark Streaming 实战案例(三) DStream Window操作
- java中的日期和时间
- 集合的增删改查
- 服务器端指标和优化方案
- HSF原理剖析