| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我自己想出来的。。。。。。同样的思路,有一小点点不同In Reply To:Re:算法!查了老长时间的资料才查出来的!不容易呀! Posted by:shengye_205 at 2007-08-18 17:26:44 也是打出它所有的完全错序环,每个环要交换的次数为数字数-1,各个环互不影响
具体代码
#include<stdio.h>
#include<cstring>
int main()
{
const int maxN=10002;
int data[maxN];
bool sorted[maxN];
int t,i,j,k,n;
int times;
int start,next;
scanf("%d",&t);
for(i=0;i<t;++i)
{
times=0;
scanf("%d",&n);
for(j=1;j<=n;++j)
scanf("%d",data+j);
memset(sorted,false,sizeof(sorted));
for(j=1;j<=n;++j)
{
if(!sorted[j])
{
sorted[j]=true;
start=j;
next=data[j];
while(next!=start)
{
sorted[next]=true;
++times;
next=data[next];
}
}
}
printf("%d\n",times);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator