Description
A group of dwarves fell into a deep trap, too short to climb up, so they decided to take a ladder. That is, a dwarf stands on the shoulders of another dwarf, knowing that the topmost dwarf can reach the trap mouth with his arms outstretched. For each dwarf, we know his height from foot to shoulder ai, and his arm length is bi. The trap depth is H. If we use the dwarf 1, the Dwarf 2, the dwarf 3,。。。 Dwarf K put a ladder to meet A1+a2+a3+....+ak+bk>=h, then the dwarf K can leave the trap to escape, once a dwarf escaped, he can no longer ladder.
We want as many dwarfs as possible to escape, asking how many dwarfs can escape.
Input first line an integer n, representing the number of dwarves, the next n rows of two integers per line of AI and Bi, the last line is H. (ai,bi,h<=10^5) Output An integer indicating how many dwarves can escape. Sample InputExample 1
2
20 10
5 5
30
Example 2
2
20 10
5 5
*Sample OutputExample 1
2
Example 2
1HINT
Data range
30% of Data n<=200
100% of Data n<=2000
First Orz Hzwer This problem is greedy after DP is normal practice: known two adjacent dwarfs A and B, consider the cost of exchanging ab position first if a under B, then the first height is a.a+b.a+b.b, the second height is a.a+ A.B Then if B is under a, then the first height is a.a+b.a+a.b, the second height is b.a+ b.b What we have to consider is the effect of the Exchange AB on the original answer. A more intuitive idea is to look at the height of the second time, because the second high-altitude one will eventually be better than the other. Also consider the possibility that the second one is higher than the other but the first one is inferior to the other. A.a+a.b>b.a+b.b may appear, but a.a+b.a+b.b<a.a+b.a+a.b, that is, a.b>a.a+b.b. To know that it is entirely possible to consider in this case, suppose a.b>a.a+b.b, that the first time in the case of non-exchange is A.A+B.A+B.B<A.B+B.A<B.B+A.B+A.A so the exchange is finished or better so just a.a+a.b< A.a+b.b can be above we have proven that the order of greed is optimal, but also to determine the maximum number of times to go to the next DP: Make F[i] said to walk the dwarf after I can also take the maximum height of such a greedy role to come out: in accordance with the order of greed to take the dwarf, can guarantee the best. Initial initialization f[0] means no dwarves go away, F[0]=σa[i]. Then it is the case that the enumeration takes to the I dwarf, which can be used to update f[j], is f[j]+ai.b>=m. So that you can calculate the maximum value of this real TM long ... It's not easy to tell the greedy.
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime> #define LL long long#define INF 0x7ffffff#define pa pair<int,int> #define Pi 3.1415926535897932384626433832795028841971using namespace Std;inline ll read () {ll x=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void Write (LL a) {if (a<0) {printf ("-"); a=-a;} if (a>=10) write (A/10);p Utchar (a%10+ ' 0 ');} struct Man{int A, b;} a[100010];inline BOOL operator < (const man &a,const man &b) {return a.a+a.b<b.a+b.b;} int N,lim,ans;int F[100010];int Main () {memset (f,-1,sizeof (f)); F[0]=0;n=read (); for (int i=1;i<=n;i++) {A[i].a=read (); A[i].b=read (); f[0]+=a[i].a;} Lim=read (); sort (a+1,a+n+1); for (int. i=1;i<=n;i++) {for (int j=ans;j>=0;j--) {if (A[i].b+f[j]>=lim) F[j+1]=max (F[J+1],F[J]-A[I].A); if (f[ans+1]>=0) ans++;}} Write (ans); return 0;}
bzoj3174 [Tjoi2013] Save the little dwarf