P1478. 陶陶摘苹果(升级版)

考点

  • 贪心

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>

using namespace std;

const int LEN = 5050;

struct Node
{
int x_, y_;
} arr[LEN];

int main()
{
int n, s, a, b;
cin >> n >> s >> a >> b;
for (int i = 0; i < n; ++i)
cin >> arr[i].x_ >> arr[i].y_;
sort(arr, arr + n, [](Node &a, Node &b) -> bool
{ return a.y_ < b.y_; });
int ans = 0, h = a + b;
for (int i = 0; i < n && s; ++i)
{
if (h >= arr[i].x_ && s >= arr[i].y_)
{
++ans;
s -= arr[i].y_;
}
}
cout << ans;
return 0;
}

思路

每次摘力气消耗最少的苹果即可