题目链接: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. unity3d Resources.Load动态加载资源

    初步整理并且学习unity3d资源加载方法,预计用时两天完成入门学习Unity3d常用两种加载资源方案:Resources.Load和AssetBundle Resources.Load就是从一个缺省 ...

  2. 多协议底层攻击工具Yesinia

    多协议底层攻击工具Yesinia   Yesinia是一款底层协议攻击工具.它提供多种运行模式,如终端文本模式.GTK图形模式.NCurses模式.守护进程模式.它利用各种底层协议的漏洞实施攻击,支持 ...

  3. spring mvc构建WEB应用程序入门例子

    在使用spring mvc 构建web应用程序之前,需要了解spring mvc 的请求过程是怎样的,然后记录下如何搭建一个超简单的spring mvc例子. 1) spring mvc的请求经历 请 ...

  4. springboot快速集成swagger

    今天技术总监说:小明,我们本次3.0改造,使用swagger2.0作为前后端分离的接口规范,它可以一键生成前后端的API,一劳永逸--小明:??? Spring Boot 框架是目前非常流行的微服务框 ...

  5. BT种子文件文件结构分析(转)

    估计80%以上接触互联网的人都知道bt是什么东西,任何一个用bt下载的人都知道这样一个概念,种子.bt种子就是记录了p2p对等网络中tracker, nodes, files等信息,也就是说,这个种子 ...

  6. 仿照jquery封装一个自己的js库

    所谓造轮子的好处就是复习知识点,加深对原版jquery的理解.本文系笔者学习jquery的笔记,记述一个名为"dQuery"的初级版和缩水版jquery库的实现.主要涉及知识点包括 ...

  7. weblogic日志管理

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

  8. 启动weblogic域不需要输入密码设置方法

    分类: IT综合技术 一.问题描述与分析     部署完WEBLOGIC后,在每次启动时执行./startWebLogic.sh脚本时,都会停在输入用户名与密码这里,相当不方便.所以要做到启动过程不输 ...

  9. Mac下安装和使用GunPG(GPG)

    GPG是加解密的工具,亦可以用于签名.非对称加解密.需要公钥和私钥. mac下安装:brew install gpg 使用gpg工具校验下载文件的完整性,从官网下载KEYS和asc文件:gpg --i ...

  10. &lt;LeetCode OJ&gt; 204. Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 分析: 思路首先:一个数不是合数就 ...