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 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.
Sample Input
Input
51 121 32 152 52 1
Output
5
Input
31 102 12 4
Output
3
题意:
给你N本书,每本书由一个厚度t[i](1或者2),宽度w[i],高度都是一样,把一些书竖着放,然后一些书横着放在同一层。问你把全部的书放好之后竖着的书的总厚度是多少?
思路:
每本书的厚度要么为1要么为2。因此我们能够依据书的厚度分为两类,然后每类按书的宽度从大到小排序,然后用二重循环进行枚举,把厚度为1的前i个和厚度为2的前j个竖着放,其它的横着放,在横着放的总宽度不超过竖着放的总厚度的前提下,求出一个竖着放总厚度最小的值来。这就是终于答案.
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define INF 0x1f1f1f1f
int a[1001];
int b[1001];
int f[1001];
int main()
{
int n,V,i,j;
while(scanf("%d",&n)!=EOF)
{
V=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&b[i],&a[i]);
V=V+b[i];
}
for(i=0;i<=V;i++)
f[i]=INF;
f[0]=0;
for(i=0;i<n;i++)
for(j=V;j>=b[i];j--)
f[j]=min(f[j],f[j-b[i]]+a[i]);
for(i=V;i>=0;i--)
{
if(V-i>=f[i])break;
}
printf("%d\n",V-i);
}
return 0;
}
Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf的更多相关文章
- 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 Round #178 (Div. 2)
A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
随机推荐
- 【php】png 图片压缩 透明底色变黑
需要使用gd库的方法 php需要引入gd扩展支持 /* * 图片压缩 ----------------------------------------------------------------- ...
- JMeter安装JSON Path Extractor插件
下载地址:https://jmeter-plugins.org/wiki/PluginsManager/ 先下载jmeter-plugins-manager-1.3.jar,点击下图中的JAR fil ...
- ubuntu14.04:php7+apache2+mysql
apache2: sudo apt-get install apache2 apache2-dev service apache2 start mysql: sudo apt-get install ...
- Zuma (区间DP)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- 注册苹果开发者账号和iOS9打包上线
链接: http://www.jianshu.com/p/507ca4e5fde0 http://blog.csdn.net/a283127993/article/details/45828175 i ...
- 【Luogu】P1013进制位(搜索)
题目链接在这里 这题和虫食算比较类似.做完这道题可以去做虫食算.都是搜索一类的题. 这样 我们分析题目可以发现进制只可能是字母的个数,也就是n-1.为什么? 因为题目要求完整的加法表才算数.如果进制低 ...
- [luoguP3110] [USACO14DEC]驮运Piggy Back(SPFA || BFS)
传送门 以 1,2,n 为起点跑3次 bfs 或者 spfa 那么 ans = min(ans, dis[1][i] * B + dis[2][i] * E + dis[3][i] * P) (1 & ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- cf468C Hack it!
Little X has met the following problem recently. Let's define f(x) as the sum of digits in decimal r ...
- day02-菜单处理
解决力度到按钮的级别 ----------------------------------------------------------------------------------------- ...