版本比较
采摘苹果代码
正文
增加 14 行删除 7 行
6 行未改动789101111121313141516171819202122212223242526272829303128323334303536373839334041424344456 行未改动using namespace std;typedef long long ll;int n,m;vector<int>a;bool check(int x){bool check(int x){ ll cost=0; for(int i=0;i<x;i++){ 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);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){ while(l<=r) { int mid=(l+r)/2; if(check(mid)){ if(check(mid)) { ans=mid; l=mid+1; } }else r=mid-1; else r=mid-1; } cout<<ans; return 0;}```