void Perestanovka_min int pos1 pos2 val1 val2 tmp cin int new int int

 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
void Perestanovka_min ()
{
int i,j,pos1,pos2,val1,val2,tmp,n;
cin>>n;
int* y=new int [n];
int* x=new int [n+1];
int* inv_x=new int [n+1];
int* d=new int [n];
for(i=0; i<n; i++)
{
x[i+1]=n-i;
inv_x[i+1]=n-i;
d[i]=1;
y[i]=0;
}
while(1)
{
for(int i=1;i<=n;i++)
cout<<x[i]<<" ";
cout<<endl;
for(i=n-1;i>=0 && (y[i]==i && d[i]==1) || (y[i]==0 && d[i]==-1);i--)
;
if(i==-1) break;
y[i]=y[i]+d[i];
pos1 = inv_x[i+1];
val1 = i+1;
pos2 = pos1 + d[i];
val2 = x[pos2];
tmp = x[pos1];
x[pos1] = x[pos2];
x[pos2] = tmp;
tmp = inv_x[val1];
inv_x[val1] = inv_x[val2];
inv_x[val2] = tmp;
for(j=i+1;j<n;j++) d[j]=-d[j];
}
delete []x;
delete []y;
delete []d;
delete []inv_x;
}