-
Notifications
You must be signed in to change notification settings - Fork 11
/
kattis_hindex.cpp
56 lines (52 loc) · 1.14 KB
/
kattis_hindex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**Kattis - hindex
* Simple BSTA question. Just do it. Note that we speed things up slightly by sorting the array and
* breaking early.
*
* Time: O(n log n), Space: O(n)
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int n;
vector<int> v;
bool works(int h) {
int ans = 0;
for (auto i : v) {
if (i >= h)
ans++;
else
break;
}
return ans >= h;
}
int main() {
fast_cin();
cin >> n;
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end(), greater<int>());
int l = 0, r = n;
while (l + 1 < r) {
int mid = (ll)(l + r) / 2;
if (works(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
if (works(r)) {
l = r;
}
cout << l << endl;
return 0;
}