-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path144A.cpp
More file actions
77 lines (71 loc) · 1.02 KB
/
144A.cpp
File metadata and controls
77 lines (71 loc) · 1.02 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
#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int t=a;
a=b;
b=t;
}
int main()
{
int a[100],n,i;
cin>>n;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int max=a[0], min=a[0];
for(i=0; i<n; i++)
{
if(a[i]<min)
{
min=a[i];
}
if(a[i]>max)
{
max=a[i];
}
}
int count =0;
int max_index, min_index;
for(i=0; i<n; i++)
if(a[i]==max)
{
max_index =i;
break;
}
for(i=n-1; i>=0; i--)
if(a[i]==min)
{
min_index =i;
break;
}
//cout<<max_index<<' '<<min_index;
for(int i=0; i<2*n; i++)
{
if(min_index==n-1 && max_index==0)
break;
if(min_index!=n-1)
{
int t = a[min_index];
a[min_index]=a[min_index+1];
a[min_index+1]=t;
min_index++;
count++;
if(max_index == min_index)
max_index--;
}
if(max_index!=0)
{
int t = a[max_index];
a[max_index]=a[max_index-1];
a[max_index-1]=t;
max_index--;
count++;
if(max_index == min_index)
min_index++;
}
}
cout<<count;
return 0;
}