其实题目不难,就是很绕,然而我现在还是有点绕在里面的。统计数出现的次数,然后按要求输出。
#include <iostream>
#include <cstring>
#include <map>
#define MAX 1000100
using namespace std;
map <int, int> no2num;
int Count[MAX];
int n;
int ans;
int main() {
cin >> n;
memset(Count, 0, sizeof(Count));
int temp;
int max = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
if (temp > max)
max = temp;
Count[temp]++;
}
int p = 0;
for (int i = 0; i <= max; i++) {
if (Count[i] != 0)
no2num[++p] = i;
}
int sum = 0;
int ans = 0;
for (int i = p; i > 0; i--) {
sum += Count[no2num[i]];
// cout << sum << " " << no2num[i] << endl;
if (sum >= no2num[i] - 1) {
ans = no2num[i] - 1;
break;
}
}
cout << ans << endl;
return 0;
}