-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa28111.cpp
More file actions
77 lines (66 loc) · 1.55 KB
/
UVa28111.cpp
File metadata and controls
77 lines (66 loc) · 1.55 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
//
// main.cpp
// UVa28111
//
// Created by Ricky on 2016/10/16.
// Copyright © 2016年 Ricky. All rights reserved.
//
#include <iostream>
using namespace std;
int data[50];
void printarr(int size )
{
//cout << &data[0];
for(int i =0 ; i < size ; i ++)
{
cout << data[i];
}
cout << endl;
}
void rec(int &sc ,int pos , int size)
{
//cout << &data[0];
if(pos <= size -1)
{
int minpos = pos;
int temp;
//cout << "sc =" << sc << endl;
for(int i = pos+1 ; i < size ; i ++)
{
if(*(data+i) < *(data+minpos)){
//cout << " min pos = " << i << endl;
minpos = i;
}
}
//cout << "pos = " << pos << endl;
for(int j = minpos ; j >= pos+1 ; j --){
temp = *(data + j);
*(data+j) = * (data + (j-1));
* (data + (j-1)) = temp;
}
minpos -= pos;
sc += minpos;
//printarr(size);
rec(sc , pos+1 , size);
}
}
int main(int argc, const char * argv[]) {
// insert code here...
int n , datanum , swapcount;
//int data[50];
cin >> n;
for(int i = 0 ; i < n ; i ++)
{
swapcount = 0 ;
cin >> datanum;
for(int j = 0 ; j < datanum ; j ++)
{
cin >> data[j] ;
}
//cout << &data[0];
rec(swapcount,0,datanum);
cout << "Optimal train swapping takes "<< swapcount << " swaps." << endl;
}
//cout << "Hello, World!\n";
return 0;
}