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;}