-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollecting_Numbers_II.cpp
64 lines (49 loc) · 924 Bytes
/
Collecting_Numbers_II.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
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define nl endl
#define ll long long int
int solve(vector<int> &nums, int m, int n)
{
vector<int> pos(n,0);
int j = 1;
for(auto i : nums)
{
pos[i] = j;
j++;
}
int ans = 1 , curr = 1;
for(int i = 1 ; i <= n ; ++i)
{
if(curr > pos[i]) ans++;
curr = pos[i];
}
return ans;
}
int main()
{
int n, m;
cin>>n>>m;
vector<int> nums(n);
for(int i = 0 ; i < n ; i++)
{
int x;
cin>>x;
nums[i] = x;
}
while(m--)
{
int a,b;
cin>>a>>b;
swap(nums[--a],nums[--b]);
cout<<solve(nums, m , n)<<endl;
}
return 0;
}