跳到正文

题解

采摘苹果代码

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

文章信息

收录
2026年7月30日
更新
2026年8月11日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

懒得写内容了。看代码你就会了!

一道二分答案题。

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
vector<int>a;
bool check(int x)
{
    ll cost=0;
    for(int i=0;i<x;i++)
    {
        int t=a[i];
        if(t>=x)continue;
        if(t+1<x)return 0;
        cost++;
    }
    return cost<=m;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    a.resize(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a.rbegin(),a.rend());
    int l=0,r=n,ans=0;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else r=mid-1;
    }
    cout<<ans;
    return 0;
}
原文