-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10327.cpp
executable file
·72 lines (56 loc) · 1019 Bytes
/
10327.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
65
66
67
68
69
70
71
72
#include <iostream>
using namespace::std ;
int main()
{
int N ;
int array[ 1001 ] = { 0 } ;
long long int data ;
long long int answer ;
int first , last , dealing ;
while( cin >> N )
{
answer = 0 ;
if( N >= 1 )
{
cin >> data ;
array[ 1 ] = data ;
}
for( int i = 2 ; i <= N ; i++ )
{
cin >> data ;
array[ i ] = data ;
if( array[ 1 ] > data )
{
first = 0 ;
}
else
{
first = 1 ;
}
if( array[ i - 1 ] > data )
{
last = i - 1 ;
while( last - first > 1 )
{
dealing = ( first + last ) / 2 ;
if( array[ dealing ] > data )
{
last = dealing ;
}
else
{
first = dealing ;
}
}
for( int j = i ; j > first + 1 ; j-- )
{
array[ j ] = array[ j - 1 ] ;
}
array[ first + 1 ] = data ;
answer += i - first - 1 ;
}
}
cout << "Minimum exchange operations : " << answer << endl ;
}
return 0 ;
}