跳到正文

题解

P3550 题解

Yizhixiaoyun约 2 分钟
原文
文章信息与版本

文章信息

发布
2022年7月12日
收录
2026年9月24日
更新
2026年11月13日
篇幅
约 2 分钟
获赞
8
题目
P3550

版本

  1. v1当前

题目传送门

题目分析

本题是一道比较有意思的贪心。

首先我们一看题目,不难发现,在过总部之前,使用距离越小的车越浪费,因此我们将距离数组从大到小排序,然后从前往后使用车即可。

但是仔细一想,如果我们把大车都用了,最后剩下的小车是没有办法带我们回家的。所以我们得留一辆可以从总部直接回家的车,然后使用刚刚的贪心策略就可以了。

贴上代码

C++
#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define ass assign
#define fst first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define zlt using
#define AK namespace
#define IOI std
zlt AK IOI;
const int maxn=500010;
long long m,d,n,a[maxn];
long long tot1,tot2,ans=1;
bool cmp(long long x,long long y){return x>y;}
void init(){
	cin>>m>>d>>n;
	for(register long long i=1;i<=n;++i) cin>>a[i];
	sort(a+1,a+n+1,cmp);
}
void duel(){
	for(register long long i=n;i>=1;--i){
		if(a[i]>=m-d){
			tot1=i;break;
		}
	}
	if(tot1==0) cout<<"0",exit(0);
	for(register long long i=1;i<=n;++i){
		if(tot1==i) continue;
		if(tot2>=d||m+d-2*tot2<=a[tot1]) break;
		else if(a[i]<=d-tot2) cout<<"0",exit(0);
		ans++;
		tot2+=(a[i]-d+tot2);
		if(tot2>=m){
			ans--;
			cout<<ans;
			exit(0);
		}
	}
}
void print(){
	if(m+d-2*tot2>a[tot1]) cout<<"0";
	else cout<<ans;
}
int main(){
	init();
	duel();
	print();
}