1704 stacked arhats

Source: Internet
Author: User

Description:

Today, Singles Day, Beihang Temple's brothers are bored again, so they get together to have fun. Considering that the direct transfer to another school affects the image of the temple, the boss decided that we would use the stacked heights
Check whether you can fulfill your wish to receive or not receive the holiday this year.
Of course, each person has a capacity value and weight. Obviously, after the stacking, the bottom person must bear the weight of all the above people. At the same time, you must bear your own weight.

Input

Number T, indicating the number of data groups
Number N represents the number of Beihang temple students
Next to N rows, there are two numbers in each row, representing the weight and load-bearing capacity of the I-th Student respectively.
(T <= 10, n <= 1111, to ensure that each student has a specific capacity value and weight)

Output

How high can he stack? (the height of each person is 1)

Sample Input

1
4
300 1000
1000 1200
200 600
100 101

Sample output

3

 

Typical dynamic planning questions

#include <iostream>#include <algorithm>#include "stdio.h"using namespace std;#define MAX 200000000struct node{    int w, l;};node a[1115];bool cmp(node a, node b){    return a.l < b.l;}int dp[1114][1114];int main(){    int t;    scanf("%d", &t);    int i, j;    while(t --)    {        int n;        scanf("%d", &n);        for(i = 1; i != n+1; i ++)            scanf("%d%d", &a[i].w, &a[i].l);        sort(a + 1, a + n + 1, cmp);        for(i = 1; i != n+1; i ++)        {            dp[0][i] = MAX;            dp[i][0] = 0;        }        dp[0][0] = 0;        for(i = 1; i != n+1; i ++)            for(j = 1; j != n+1; j ++)            {                if(dp[i-1][j] < dp[i-1][j-1]+a[i].w)                    dp[i][j] = dp[i-1][j];                else                {                    dp[i][j] = dp[i-1][j-1] + a[i].w;                    if(dp[i][j] > a[i].l)                        dp[i][j] = dp[i-1][j];                }            }        for(i = n; i != -1; i --)            if(dp[n][i] != MAX)                break;        printf("%d\n", i);    }    return 0;}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.