having snack
There are bags of snacks on the table n, different snacks have different degrees of decay and delicious wi degree di, each snack in a unit of time will decrease the extent of delicious di, but does not drop below zero.
qwb per unit time can eat a bag of snacks. Now qwb you want maximum degree after eating all the delicious snacks available. Q. What is the maximum?
Input
The first line, an integer n, n representatives snack bags next n lines, each line an integer of 2 wi and di (1 <= n <= 100,000), (0 <= wi <= 1,000,000,000), (0 <= di <= 10,000)
Output
Output line, maximum deliciousness.
4
5 3
4 3
5 4
7 5
9
The subject is a greedy, but still feel a little difficult, and subject to decay speed of greed, because the rate of decay of the representative value of the loss delicious,
So we have to try to choose a delicious loss is small, but if the value of a delicious snack has less than its rate of decay, and this time it is necessary to change the rate of decay.
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <map> #include <queue> #include <vector> #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int maxn = 1e5 + 10; struct node { ll w, d, t; node(ll w=0,ll d=0,ll t=0):w(w),d(d),t(t){} bool operator<(const node &a)const { if (a.d == d) return a.w > w; return a.d > d; } }; int main() { int n; scanf("%d", &n); priority_queue<node>que; for (int i = 1; i <= n; i++) { ll w, d; scanf("%lld%lld", &w, &d); que.push(node(w, d, 0)); } ll day = 0, ans = 0; while(!que.empty()) { node e = que.top(); que.pop(); ll num = e.w - (day - e.t)*e.d; if (num <= 0) continue; if (num < e.d) { ll w = num, d = num, t = day; que.push(node(w, d, t)); continue; } ans += num; day++; } printf("%lld\n", ans); return 0; }