Little Pony and Permutation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 639 Accepted Submission(s): 342
Problem Description
Input
Input contains multiple test cases (less than 10). For each test case, the first line contains one number n (1<=n<=10^5). The second line contains n numbers which the i-th of them(start from 1) is σ(i).
Output
For each case, output the corresponding result.
Sample Input
5 2 5 4 3 1 3 1 2 3
Sample Output
(1 2 5)(3 4) (1)(2)(3)
= = 把a数组设为bool 型,无限WA。。。我真是。。。本来1A的
题意:就是找循环。比如说 1 ->2 -> 5->1 所以 (1,2,5)是一个循环。
#include#include #include #include #include #include using namespace std;const int N = 100005;bool vis[N];int res[N],a[N];int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } memset(vis,false,sizeof(vis)); for(int i=1;i<=n;i++){ int j=i; if(vis[j]) continue; int id = 0; while(!vis[j]){ res[id++] = j; vis[j]=true; j = a[j]; } printf("("); for(int i=0;i