Replacement group and Rotation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.