题目链接:http://codeforces.com/contest/294/problem/B

B. Shaass and Bookshelf
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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).

Output

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

Examples
input
5
1 12
1 3
2 15
2 5
2 1
output
5
input
3
1 10
2 1
2 4
output
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的更多相关文章

  1. 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 ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  3. Codeforces Round #178 (Div. 2)

    A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...

  4. 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 ...

  5. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

随机推荐

  1. Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa

    原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...

  2. upper_bound——自己的实现

    int BSearch() { int ln(1),rn(n+1); while(ln+1<rn) { int mid=(ln+rn)>>1; if (Check(mid)) { l ...

  3. weblogic日志管理

    服务器日志  每个 WebLogic Server 实例将来自子系统和应用程序的所有消息写入位于本地主机上的服务器日志文件.默认情况下,服务器日志文件位于服务器实例根目录下的 logs 目录中: 例如 ...

  4. iOS -- MBProgressHUB

    高级: http://www.jianshu.com/p/485b8d75ccd4 //只有小菊花 - (void)indeterminateExample { // Show the HUD on ...

  5. Neutron网络入门

    Neutron是OpenStack核心项目之中的一个,提供云计算环境下的虚拟网络功能.Neutron的功能日益强大,并在Horizon面板中已经集成该模块.作为Neutron的核心开发人员之中的一个. ...

  6. Chrome内核保存为mhtml(单网页)

    在地址栏输入:chrome://flags  回车 然后Ctrl+f查找mhtml Tips: 如果网页图片看不太清可以CTRL+鼠标滚轮放大网页 如果系统原因以及其它因素可以下载:QQ浏览器(默认保 ...

  7. vue 给组件绑定原生事件

    有时候,你可能想在某个组件的根元素上监听一个原生事件.可以使用 v-on 的修饰符 .native.例如: <my-component v-on:click.native="doThe ...

  8. 【java读书笔记】——Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)

    两个月之前准备软考时,简单的从理论上总结了最经常使用的数据结构和算法,比方:线性表,链表,图.在进行java开发时,jdk为我们提供了一系列对应的类来实现主要的数据结构.jdk所提供的容器API位于j ...

  9. 《C程序猿:从校园到职场》出版预告(4):从“散兵游勇”到“正规部队”

    看过电视剧<楚汉传奇>的朋友应该对这个场景还有印象:当刘邦第一次去找项羽帮忙的时候.他们一行人看到了项羽军营是怎样练兵的.想到自己练兵的方法,当时就震惊了."刘家军"就 ...

  10. PCB板布线中地线和电源线的布线规则

    电源. 地线的布置考虑不周到而引起干扰,使产品的性能下降,严重时会降低产品的成功率.要把电源线和地线处理好,将电源线和地线所产生的噪音干扰降到最低限度,以保证产品的质量.一.电源线和地线的布线规则1) ...