MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / main

Function main

CSES/Sorting/CollectionNumbers2.cc:5–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3using namespace::std;
4
5signed main(){
6 ios_base::sync_with_stdio(false),cin.tie(nullptr);
7
8 int n,q; cin >> n >> q;
9 std::vector<int> v(n);
10 for (int &x: v) cin >> x, --x;
11 while (q--) {
12 int a, b; cin >> a >> b;
13 --a,--b;
14 swap(v[a],v[b]);
15 map<int,int>m;
16 int ans = 1;
17
18 for (int i = 0; i < n; ++i) { m[v[i]] = i; }
19 for (int i = 1; i < n; ++i) {
20 ans += (m[i] < m[i-1]);
21 }
22 cout << ans << endl;
23 }
24 return 0;
25}
26
27// bruteforce
28// 4 2 1 5 3

Callers

nothing calls this directly

Calls 1

swapFunction · 0.50

Tested by

no test coverage detected