-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10775.cpp
87 lines (75 loc) · 1.77 KB
/
10775.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
int g[100010], gn, pn, arr[100010], ans;
int Find(int v)
{
if (g[v] == v) return v;
return g[v] = Find(g[v]);
}
void Union(int l, int r)
{
int ll = Find(l);
int rr = Find(r);
if (ll > rr) g[ll] = rr;
else g[rr] = ll;
}
int main (){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> gn >> pn;
for(int i = 0; i <= gn; i++) g[i] = i;
for(int i = 0; i < pn; i++){
cin >> arr[i];
}
for(int i = 0; i < pn; i++){
int pt = Find(arr[i]);
if(pt != 0){
Union(pt, pt-1);
//cout << pt << endl;
ans++;
}
else{
break;
}
}
cout << ans;
}
// TLE code (Brute Force)
// #include <bits/stdc++.h>
// using namespace std;
// int g, p, arr[100010], cnt, visited[100010], limit = 0;
// int main (){
// ios_base::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
// cin >> g >> p;
// for(int i = 0; i < p; i++){
// cin >> arr[i];
// }
// for(int i = 0; i < p; i++){
// int fl = 0;
// if(visited[arr[i]] == 0){
// visited[arr[i]] = 1;
// fl = 1;
// //cout << arr[i] << fl << '\n';
// }
// else{
// int ptr = arr[i];
// while(visited[ptr] == 1){
// ptr--;
// fl = 2;
// //cout << arr[i] << fl << '\n';
// }
// visited[ptr] = 1;
// if(ptr == 0){
// fl = 3;
// //cout << arr[i] << fl << '\n';
// limit = i;
// break;
// }
// }
// limit = i+1;
// }
// cout << limit << '\n';
// }