#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 7;
int n;
int a[N];
bool vis[N];
set<int> s[N];
int h = 0;
int get_nxt(int pos, int val) {
pos++;
auto it = s[val].lower_bound(pos);
if (it == s[val].end()) {
return -1;
}
return *it;
}
void dfs(int i) {
vis[i] = 1;
--h;
int j = get_nxt(i, h);
if (h == 0) {
return;
}
if (j == -1) {
j = i;
--h;
}
if (h == 0) {
return;
}
dfs(j);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
s[a[i]].insert(i);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
h = a[i];
dfs(i);
++ans;
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
49756 KB |
Output isn't correct |
2 |
Incorrect |
12 ms |
49756 KB |
Output isn't correct |
3 |
Incorrect |
13 ms |
50092 KB |
Output isn't correct |
4 |
Incorrect |
14 ms |
50008 KB |
Output isn't correct |
5 |
Execution timed out |
2067 ms |
92444 KB |
Time limit exceeded |
6 |
Execution timed out |
2079 ms |
97104 KB |
Time limit exceeded |
7 |
Execution timed out |
2041 ms |
88932 KB |
Time limit exceeded |
8 |
Execution timed out |
2028 ms |
88404 KB |
Time limit exceeded |
9 |
Execution timed out |
2037 ms |
90708 KB |
Time limit exceeded |
10 |
Execution timed out |
2066 ms |
92160 KB |
Time limit exceeded |