为什么简单贪心不行
查看原帖
为什么简单贪心不行
690243
houluyu楼主2024/11/18 17:36

如题 收益相同,感觉可以直接排序啊

#include<bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
#define ll long long
using namespace std;

const int N = 150005;
int n, q;
pii a[N];

bool cmp(pii x, pii y)
{
	if(x.s == y.s)return x.f < y.f;
	else return x.s < y.s;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i ++)
	{
		cin >> a[i].f >> a[i].s;
	}
	sort(a + 1, a + 1 + n, cmp);
	int lst = 0, cnt = 0;
	for(int i = 1; i <= n; i ++)
	{
		int st = a[i].s - a[i].f;
		if(st < lst)continue;
		lst = lst + a[i].f;
		cnt ++;
	}
	cout << cnt << endl;
	return 0;
}

2024/11/18 17:36
加载中...