跳到正文

题解

题解:AT_abc437_c [ABC437C] Reindeer and Sleigh 2

NaPF6约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年12月22日
收录
2025年12月22日
更新
2026年11月10日
篇幅
约 1 分钟

版本

  1. v1当前

很简单的贪心。

我们可以维护在雪橇上的驯鹿的重量与拉雪橇的驯鹿的力量之差 dd。

先假设所有驯鹿都在雪橇上,此时 dd 就为 ∑Wi\sum W_i。

我们的目的是让 d≤0d\le0,而当我们让雪橇上的驯鹿 ii 拉雪橇时,dd 就会减少 Wi+PiW_i+P_i,所以我们直接贪心选取 Wi+PiW_i+P_i 最大的几只驯鹿即可。

Code
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=3e5+5;
ll T,n,d,ans;
struct node{
	ll w,p;
}a[N];
bool cmp(node x,node y){
	return x.w+x.p>y.w+y.p;
}
void solve(){
	d=0;
	cin>>n;ans=n;
	for(int i=1;i<=n;i++){
		cin>>a[i].w>>a[i].p;
		d+=a[i].w;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++){
		d-=a[i].w+a[i].p;
		ans--;
		if(d<=0)break;
	}
	cout<<ans<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>T;
	while(T--)solve();
	return 0;
}
原文