-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLilyHomework.cpp
More file actions
79 lines (59 loc) · 1.2 KB
/
LilyHomework.cpp
File metadata and controls
79 lines (59 loc) · 1.2 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
77
78
79
// problem: "https://www.hackerrank.com/challenges/lilys-homework/problem"
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int>V;
map<int,int>M1,M2;
int i;
for(i=0;i!=n;i++)
{
int x;
cin>>x;
V.push_back(x);
M1[x] = i;
M2[x] = i;
}
vector<int>C1(V); // ascending order
vector<int>C2(V); // descending order
sort(V.begin(),V.end());
int s1 = 0;
for(i=0;i!=n;i++)
{
if(C1[i]!=V[i])
{
int x;
x = M1[V[i]];
swap(C1[i],C1[x]);
M1[C1[x]] = x;
s1++;
}
}
// desending order
sort(V.begin(),V.end(),greater<int>());
// for(i=0;i!=n;i++)
// cout<<V[i]<<' ';
// cout<<'\n';
int s2 = 0;
for(i=0;i!=n;i++)
{
if(C2[i]!=V[i])
{
int x;
x = M2[V[i]];
swap(C2[i],C2[x]);
M2[C2[x]] = x;
s2++;
}
}
cout<<min(s1,s2);
// cout<<s1<<' '<<s2;
return 0;
}