B. Shaass and Bookshelf
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard 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 abve 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 limit ≤ limit n Limit ≤ limit 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 limit ≤ limit 2, when 1 Gbit/s ≤ Wi-Fi ≤ 0000100 ).
Output
On the only line of the output print the minimum total thickness of the vertical books that we can achieve.
Sample test (s)
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
I have never written about ACM for a long time, and I have been retiring for more than a year.
Codeforces was not as popular as it was before when I was working on ACM. At that time, we used to click topcoder.
This codeforces was a sudden whim. I thought there was nothing else to do.
This is div2's B Question. I first began to think about greed, but I didn't handle several special situations for a long time. Then I began to think about the direction of DP.
Since DP was always weak when I was studying ACM, I haven't figured out the transition equation of the State for a long time.
There is no way at last, just use the only option: memory-based search
Yes. If this question is used for brute force search, it is actually quite easy to write. You only need to enumerate the situations where each book is placed below or above.
Of course, if you don't need to memorize the data and use recursive data directly, the data in the ninth Group will use TLE.
My code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
Int w [1, 105], t [2, 105], n;
Int mem [105] [205] [205];
Int min (int a, int B)
{
If (a> B)
Return B;
Else
Return;
}
Int dfs (int pos, int thi, int whi)
{
If (pos = n)
Return thi;
If (mem [pos] [thi] [whi]! =-1)
Return mem [pos] [thi] [whi];
If (thi-t [pos]> = whi + w [pos])
Return mem [pos] [thi] [whi] = min (dfs (pos + 1, thi-t [pos], whi + w [pos]), dfs (pos + 1, thi, whi ));
Return mem [pos] [thi] [whi] = dfs (pos + 1, thi, whi );
}
Int main ()
{
Int I, sum;
While (scanf ("% d", & n )! = EOF)
{
Memset (mem,-1, sizeof (mem ));
Sum = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & t [I], & w [I]);
Sum = sum + t [I];
}
Printf ("% d \ n", dfs (0, sum, 0 ));
}
Return 0;
}