[BestCoder Round #7] hdu 4985 Little Pony and Permutation (找循环节)

Little Pony and Permutation

Problem Description

As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony. Being familiar with composition and decomposition is the fundamental course for a young unicorn. Twilight Sparkle is interested in the decomposition of permutations.
A permutation of a set S = {1, 2, ..., n} is a bijection from S to itself. In the great magician —— Cauchy‘s two-line notation, one lists the elements of set S in the first row, and then for each element, writes its image under the permutation below it in
the second row. For instance, a permutation of set {1, 2, 3, 4, 5} σ can be written as:

Here σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3, and σ(5) = 1.

Twilight Sparkle is going to decompose the permutation into some disjoint cycles. For instance, the above permutation can be rewritten as:

Help Twilight Sparkle find the lexicographic smallest solution. (Only considering numbers).

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)

Source

BestCoder Round #7

解题思路:

找循环节,用while循环就可以了,注意输出格式。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=100010;
int num[maxn];
bool vis[maxn];
int n;

void getCircle()
{
    memset(vis,0,sizeof(vis));
    int t;
    bool ok=0;
    for(int i=1;i<=n;i++)
    {
        int temp=i;
        if(!vis[temp])
            printf("(");
        while(!vis[temp])
        {
            t=temp;
            vis[temp]=true;
            temp=num[temp];
            if(!vis[temp])
                printf("%d ",t);
            else
            {
                printf("%d",t);
                ok=1;
            }
        }
        if(ok)
        {
             printf(")");
             ok=0;
        }
    }
    printf("\n");
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        getCircle();
    }
    return 0;
}
时间: 2024-08-10 19:08:52

[BestCoder Round #7] hdu 4985 Little Pony and Permutation (找循环节)的相关文章

HDU 4985 Little Pony and Permutation(置换)

HDU 4985 Little Pony and Permutation 题目链接 题意:给定一个置换,输出分解成的循环 水题,直接模拟即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 100005; int n, a[N], vis[N]; int main() { whi

HDU 4985 Little Pony and Permutation(数学 置换群)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 置换群:http://baike.baidu.com/view/1879054.htm?fr=aladdin Little Pony and Permutation Problem Description As a unicorn, the ability of using magic is the distinguishing feature among other kind of pony

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

[BestCoder Round #4] hdu 4931 Happy Three Friends

Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 70    Accepted Submission(s): 62 Problem Description Dong-hao , Grandpa Shawn , Beautful-leg Mzry are good friends. One day ,

[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 743    Accepted Submission(s): 275 Problem Description Hanamichi is taking part in a programming contest, and he is assigned to so