-
Notifications
You must be signed in to change notification settings - Fork 0
/
flip.cpp
91 lines (73 loc) · 1.26 KB
/
flip.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>
using namespace std;
int mod(int x)
{
if(x>=0)
return x;
return -x;
}
bool identical(vector <int> a, vector <int> b)
{
for(int i=0; i<a.size(); i++){
if( a[i]!= b[i] )
return false;
}
return true;
}
void flip(int n, vector <int> &arr){
vector <int> cp(arr.begin(), arr.end());
for(int i=0; i<n; i++)
arr[i]=-cp[n-i-1];
}
int main()
{
int cases, ct=0, tmp, n;
cin >> cases;
vector < vector <int> > vv;
while(ct<cases){
cin>>n;
vector <int> flips;
vector <int> chk, chk1, arr;
for(int i=0; i<n; i++){
cin>>tmp;
arr.push_back(tmp);
}
vector <int> brr(arr.begin(), arr.end());
for(int i=0; i<n; i++){
chk.push_back(n-i);
chk1.push_back(i+1);
}
int i,j;
for(int i=0; i<n; i++){
if(identical(brr, chk1)){
break;
}
for(j=0; j<n; j++){
if(mod(brr[j])==chk[i])
break;
}
if(!(j==n-1-i && brr[j] > 0)){
if(j!=0){
flips.push_back(j+1);
flip(j+1, brr);
}
if(brr[0]>0){
flips.push_back(1);
flip(1, brr);
}
flips.push_back(n-i);
flip(n-i, brr);
}
}
vv.push_back(flips);
ct++;
}
for(int i=0; i<vv.size(); i++){
cout<<i+1<<" "<<vv[i].size()<<" ";
for(int j=0; j<vv[i].size(); j++)
cout<<vv[i][j]<<" ";
cout<<endl;
}
return 0;
}