-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-12488.cpp
More file actions
42 lines (34 loc) · 1.12 KB
/
UVa-12488.cpp
File metadata and controls
42 lines (34 loc) · 1.12 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
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
int arr[30];
int end1[30];
int main() {
int N;
int i, j, k;
while (scanf("%d", &N) != EOF) {
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
for (int i = 0; i < N; i++) {
cin >> end1[i];
}
// loop until you find the index of the element you are at.
// change arr[k] to arr[k-1] for all k from j: (index of element in end1) to k > i.
// each time you change a value => increment ans.
// that's how you find how many swaps it will take so that arr becomes end1 or the opposite.
int ans = 0;
for (i = 0; i < N; i++) {
for (j = i; j < N; j++) {
if (arr[j] == end1[i])
break;
}
for (k = j; k > i; k--) {
arr[k] = arr[k-1];
ans++;
}
}
printf("%d\n", ans);
}
}