forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1162 - Sorting Methods.cpp
More file actions
76 lines (66 loc) · 1.71 KB
/
1162 - Sorting Methods.cpp
File metadata and controls
76 lines (66 loc) · 1.71 KB
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
// Sorting Methods
//
// Problem name: Sorting Methods
// Problem Link: https://cses.fi/problemset/task/1162
// Author: Bernardo Archegas (https://codeforces.com/profile/Ber)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const ll INF = 1e18;
int v[MAXN], a[MAXN], inv[MAXN], vis[MAXN];
vector<int> adj[MAXN];
int sum(int pos) {
int ans = 0;
for (; pos; pos -= (pos & -pos)) ans += a[pos];
return ans;
}
void upd(int pos, int val) { for (; pos < MAXN; pos += (pos & -pos)) a[pos] += val; }
void dfs(int node) {
vis[node] = 1;
for (int x : adj[node])
if (!vis[x]) dfs(x);
}
int lis(int n) {
vector<int> pilha;
for (int i = 0; i < n; i++) {
auto pos = lower_bound(pilha.begin(), pilha.end(), v[i]);
if (pos == pilha.end())
pilha.push_back(v[i]);
else *pos = v[i];
}
return pilha.size();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
ll ans1 = 0;
int ans2 = n, ans3 = n, ans4 = n;
int atual = n;
for (int i = n-1; i >= 0; i--) {
ans1 += sum(v[i]);
upd(v[i], 1);
if (v[i] == atual) {
atual--;
ans4--;
}
adj[v[i]].push_back(i+1);
adj[i+1].push_back(v[i]);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i);
ans2--;
}
}
ans3 -= lis(n);
cout << ans1 << ' ' << ans2 << ' ' << ans3 << ' ' << ans4 << '\n';
return 0;
}