Question 1 of bestcoder last night:
At the beginning, I watched Alibaba Cloud 2333333 for half a day.
In fact, it is used to rotate the replacement group.
For example
O (1) = 2, O (2) = 5, O (3) = 4, O (4) = 3, O (5) = 1
Among them, O (1) = 2, O (2) = 5, O (5) = 1 is a rotation. After turning around, it returns 233 again.
Similarly, O (3) = 4, O (4) = 3 is a rotation.
Therefore, the output result is (1 2 5) (3 4)
---------------------------------------------------------------------------------
I don't know why my code is always TLE t ^ t.
I finally know why t ^ t
While (~ Scanf (......))
Note that ~
Because after reading on OJ, scanf returns a value of-1 (that is, binary 11111111 ~ You can end the loop.
Hidden bugs... cannot be found at hand
Code attachment
1 #include "stdio.h" 2 int a[100010]; 3 int v[100010]; 4 int tx,ty,n,i; 5 6 int main() 7 { 8 while (~scanf("%d\n",&n)) 9 {10 for (i=1;i<=n;i++)11 {12 scanf("%d",&a[i]);13 v[i]=0;14 }15 for (i=1;i<=n;i++)16 {17 if (v[i]==0)18 {19 printf("(%d",i);20 v[i]=1;21 tx=i;22 ty=a[i];23 while (v[ty]==0)24 {25 printf(" ");26 v[ty]=1;27 tx=ty;28 ty=a[ty];29 printf("%d",tx);30 }31 printf(")");32 }33 }34 printf("\n");35 }36 return 0;37 }View code
Orz
Replacement group and Rotation