AcWing.3745 牛的学术圈 I
题目链接
题目思路
将问题转换成,有一个数组\(a[N]\),找到一个特殊的数idx,满足至少有idx个数 >= idx a[i] >= i (idx求最大).并且给定一个距离l,使得这个区间的数字可以+1.
这样就可以以倒序来存储数字,然后找到最大的符合条件的idx.
因为每个数最多+1,所以当a[idx + 1] < idx 时, idx + 1 的特殊数肯定不会满足条件的,所以直接输出idx即可
若满足条件,则遍历1 ~ idx + 1 当最终l比这个区间的数大的时候表示特殊的数可以更新,则输出idx + 1 否则输出idx
题目代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, l;
int thesis[N];
bool cmp(int x, int y)
{
return x > y;
}
int main()
{
cin >> n >> l;
for(int i = 1; i <= n; i ++ )
cin >> thesis[i];
sort(thesis + 1, thesis + n + 1, cmp);
int idx = 0;
for(int i = 1; i <= n; i ++ )
{
if(thesis[i] >= i) idx = i;
else break;
}
if(thesis[idx + 1] < idx)
{
cout << idx << endl;
return 0;
}
int cnt = 0;
for(int i = 1; i <= idx + 1; i ++ )
if(thesis[i] == idx) cnt++;
if(l >= cnt) cout << idx + 1 << endl;
else cout << idx << endl;
return 0;
}
孤独本是常态

浙公网安备 33010602011771号