设在雪橇上的驯鹿集合为 ,则必满足 。
。
。
明显可以使用贪心,将原数组按照从小到大排序,每次取 最小的,直到和大于 为止。
代码:
#include <bits/stdc++.h>
using namespace std;
struct node{
int w, p;
}a[300005];
int t, n;
bool operator < (node a, node b){
return a.w + a.p < b.w + b.p;
}
int main(){
cin >> t;
while(t--){
cin >> n;
long long sum = 0;
for(int i = 1; i <= n; i++){
cin >> a[i].w >> a[i].p;
sum += a[i].p;
}
sort(a + 1, a + n + 1);
int cnt = 0;
for(int i = 1; i <= n; i++){
if(sum >= a[i].w + a[i].p){
cnt++;
sum -= a[i].w + a[i].p;
}
}
cout << cnt << "\n";
}
return 0;
}