集训第四周(高效算法设计)I题 (贪心)

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

51 121 32 152 52 1

Output

5

Input

31 102 12 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;
}
时间: 2024-07-29 04:24:40

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

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

原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,接下来的选择是快排,归并,看你喜欢了 这里列出归并的解法: #include"iostream" using namespace std; const int maxn=500000+10; int T[maxn]; int a[maxn]; long long sum; void merg

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

原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度为10,你先找5,去枚举每一个区间为5的连续的数,发现存在这样的数,那么就可以继续往左找,反之则往右找,直到左右区间重合,即为正确答案,(有可能是右区间小于左区间,所以每次都应该求区间中点值) #include"iostream" #include"set" #incl

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

原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数为a[0],之后遇到比a[0]大的数就更新它,再拿这个被减数去减数组中的每一个元素,同时也要不断地更新这个m值. #include"iostream" #include"set" #include"cstring" #include"cst

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

Description There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<tex2html_verbatim_mark> . The N<tex2html_verbatim_mark> marbles are put in a circular track in an arbitrary order. In the top part of the track there

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

A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these integers. Your task is to generate a given permutation from the initial arrangement 1, 2, 3, . . . , n using only two simple operations. •  Operation 1: You

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

Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of songs on his tapes. For a given tape and for each song on that tape John knows the length of the song and the frequency of playing that song. His probl

集训第四周(高效算法设计)K题 (滑窗问题)

UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include"iostream" #include"set" #include"cstring" #include"cstdio" #include"algorithm" using namespace std; const

集训第四周(高效算法设计)J题 (中途相遇法)

Description The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_verbatim_mark> of integer values, compute how many quadruplet (a, b, c, d ) AxBxCxD<tex2html_verbatim_mark> are such that a + b + c + d = 0<tex2h

集训第四周(高效算法设计)G题 (贪心)

G - 贪心 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of th