1 条题解

  • 0
    @ 2026-9-24 23:05:11

    题目传送门

    题目分析

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

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

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

    贴上代码

    #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();
    }
    
    • 1

    信息

    ID
    5084
    时间
    1000ms
    内存
    264MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者