Description

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
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3

如题,书你可以竖着放,横着放(必须放在竖着放的书的上面),要求竖着放的书所造成的厚度越小越好
既然书的厚度只有1 2两种情况,那么就可以准备两个数组一个放厚度为1的一个放厚度为2的,最后贪心,先对两个数组排序,长度大的在前面,因为你竖着放的话,长度多大毫无关系,横着放的话,长度就很是问题了。然后你就可以从小到大开始枚举,选出几本放在下面,计算出这几本的厚度,再把余下的书都默认放在上面,计算出它们的长度,如果厚度大于等于长度,退出循环,输出答案
#include"iostream"
#include"algorithm"
using namespace std;
const int maxn=100+10;
int n;
int f,fb; int a[maxn];
int b[maxn]; bool cmp(int a1,int a2)
{
return a1>a2;
} int suma[maxn],sumb[maxn]; void Init()
{
int tt,ttt;
f=fb=0;
for(int i=0;i<n;i++)
{
cin>>tt>>ttt;
if(tt==1) a[f++]=ttt;
if(tt==2) b[fb++]=ttt;
}
sort(a,a+f,cmp);
sort(b,b+fb,cmp);
suma[0]=0;sumb[0]=0;
for(int i=1;i<=f;i++) suma[i]=a[i-1]+suma[i-1];
for(int i=1;i<=fb;i++) sumb[i]=b[i-1]+sumb[i-1];
} void Work()
{
int ans=100000;
for(int i=0;i<=f;i++)
for(int j=0;j<=fb;j++)
{
int thick=i+j*2;
int width=suma[f]-suma[i]+sumb[fb]-sumb[j];
if(thick>=width&&ans>thick) ans=thick;
}
cout<<ans<<endl;
} int main()
{
cin>>n;
Init();
Work();
return 0;
}

集训第四周(高效算法设计)I题 (贪心)的更多相关文章

  1. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

  2. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  3. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  4. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  5. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  6. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  7. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  8. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  9. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

随机推荐

  1. Hihocoder [Offer收割]编程练习赛70 解题报告 By cellur925

    并没有第四题.(还不会矩阵乘法加速线性数列) 题目1 : 数位翻转 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 n,你可以进行若干次操作,每次操作可以翻转 ...

  2. django 网站项目测试

    视图和 URL 配置: 在先前创建的 meishiweb目录下的 meishiweb 目录新建一个 view.py 文件,并输入代码: 此时在浏览器即可访问: 证明已经成功 我们也可以修改成以下的规则 ...

  3. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  4. Linux的proc文件系统 分类: linux 2014-06-02 10:21 623人阅读 评论(0) 收藏

    proc为一个内核数据结构接口,用户空间和内核空间可以通过该接口通信, 与普通文件不同的是,这些虚拟文件的内容都是动态创建的. proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间. ...

  5. GOTO语句以及GOTO机制的模式实现

    goto语句提供了方法内部的任意跳转,它在特殊场景下被应用. 而假设一个对象执行一个方法后,我们期望其余任何对象都可以捕获它,然后自己执行某些操作,那么可以怎么实现呢 class 皇宫 { void ...

  6. AJPFX讲解java单例模式

    单例设计模式概述:      单例模式就是要确保类在内存中只有一个对象,该实例必须自动创建,并且对外提供单例模式有以下特点: 1.单例类只能有一个实例. 2.单例类必须自己自己创建自己的唯一实例. 3 ...

  7. Java文件上传(基础性)

    /** * * 上传文件 * */ public class FileUploadServlet2 extends HttpServlet { protected void doGet(HttpSer ...

  8. java之java.sql.SQLException: ResultSet is from UPDATE. No Data.

    问题解释:java调用存储过程的时候,查询结果不能通过ResultSet来查询,需要通过CallableStatement来查询, 比如: ResultSet rs = callableStateme ...

  9. CentOS7搭建LAMP

    阿里云CentOS7.3搭建 Apache+MySQL+PHP环境 参考https://www.cnblogs.com/apro-abra/p/4862285.html   一.安装Apache 1. ...

  10. linux下mysql开启可访问

    修改mysql配置连接信息 将bind-address注释 vim /etc/my.cnf 修改mysql用户授权 mysql>GRANT ALL PRIVILEGES ON *.* TO ' ...