比赛 |
20120418s |
评测结果 |
EAAATTTTTT |
题目名称 |
排序 |
最终得分 |
30 |
用户昵称 |
Citron酱 |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2012-04-18 11:06:31 |
显示代码纯文本
#include <cstdio>
#include <cstdlib>
#define I_F "sorta.in"
#define O_F "sorta.out"
const short MAXn=24;
const short MAXs=100;
const short P=20;
struct que
{
int s[MAXn];
short d,x;
que *father, *next;
};
short n;
int a[MAXn], b[MAXn];
short ans;
short s[MAXs];
void Input();
void Cpy(const int*, int*, const short=0);
template<typename Any>
inline void Swap(Any&, Any&);
void Sort(int*, const short&, const short&);
bool Compare(const int *a, const int *b);
void Search();
void Output();
int main()
{
Input();
Sort(b,0,n-1);
Search();
Output();
return 0;
}
void Input()
{
freopen(I_F,"r",stdin);
scanf("%hd",&n);
for (short i=0; i<n; scanf("%d",&a[i++]));
Cpy(a,b);
}
void Cpy(const int *x, int *y, const short z)
{
for (short i=0; i<=z; i++)
y[i]=x[z-i];
for (short i=z+1; i<n; i++)
y[i]=x[i];
}
template<typename Any>
inline void Swap(Any &a, Any &b)
{
Any t=a;
a=b;
b=t;
}
void Sort(int *s, const short &l, const short &r)
{
if (r-l>P)
{
int x=s[l+rand()%(r-l+1)];
short i=l, j=r;
do
{
while (s[i]<x) ++i;
while (s[j]>x) --j;
if (i<j)
Swap(s[i++],s[j--]);
} while (i<j);
if (i<r) Sort(s,i,r);
if (l<j) Sort(s,l,j);
}
else
{
bool flag=true;
for (short i=l; i<r && flag; ++i)
{
flag=false;
for (short j=r; j>i; --j)
if (s[j]<s[j-1])
Swap(s[j],s[j-1]),
flag=true;
}
}
}
bool Compare(const int *a, const int *b)
{
for (short i=0; i<n; i++)
if (a[i]!=b[i])
return false;
return true;
}
void Search()
{
que *h[2], *t[2], *p[2];
h[0]=new que;
Cpy(a,h[0]->s);
h[0]->x=0;
h[0]->d=0;
h[0]->father=h[0]->next=NULL;
t[0]=h[0];
h[1]=new que;
Cpy(b,h[1]->s);
h[1]->x=0;
h[1]->d=0;
h[1]->father=h[1]->next=NULL;
t[1]=h[1];
bool flag=true, flag2;
short I, J;
for (short i=0; flag; i++)
{
flag2=true;
I=i%2;
J=1-I;
while (flag2 && flag)
{
for (short j=1; j<n && flag; j++)
if (j!=h[I]->x)
{
t[I]->next=new que;
t[I]=t[I]->next;
t[I]->father=h[I];
t[I]->next=NULL;
t[I]->x=j;
t[I]->d=h[I]->d+1;
Cpy(h[I]->s,t[I]->s,j);
for (que *k=h[J]; k!=NULL && flag; k=k->next)
if (Compare(t[I]->s,k->s))
{
p[I]=t[I];
p[J]=k;
flag=false;
}
}
h[I]=h[I]->next;
flag2=(h[I]->d==h[I]->father->d);
}
}
for (que *i=p[0]; i->x>0; i=i->father)
s[i->d-1]=i->x+1;
ans=p[0]->d;
for (que *i=p[1]; i->x>0; i=i->father)
s[ans++]=i->x+1;
}
void Output()
{
freopen(O_F,"w",stdout);
printf("%hd\n",ans);
for (short i=0; i<ans-1; printf("%hd ",s[i++]));
printf("%hd\n",s[ans-1]);
}