HDU 4985-Little Pony and Permutation(模擬置換)

kewlgrl發表於2017-03-20

Little Pony and Permutation

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 37   Accepted Submission(s) : 14
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

題目意思:

有1~N,給出這N個數的置換,判斷哪些數是同一範圍內的,用括號將同一範圍內的數括起來。


解題思路:

用a[i]表示數i的置換數,比如題目給的2 5 4 3 1,表示a[1]=2,a[2]=5,a[3]=4,a[4]=3,a[5]=1.
因為要求字典序的解,所以我們從a[1]開始,每次找到a[i]表示的數然後標記其是否訪問過,

例如a[1]=2,a[2]=5,a[5]=1,此時a[1]=2已經訪問,所以這1,2,5三個數是同一個範圍內的。


#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100010
#define INF 0x3f3f3f3f
int a[MAXN];
bool vis[MAXN];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
    //freopen("G:/cbx/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    while(cin>>n)
    {
        memset(vis,false,sizeof(vis));
        for(int i=1; i<=n; ++i)
            cin>>a[i];
        for(int i=1; i<=n; ++i)
        {
            bool s=true,e=true;//前後括號標記
            int cnt=0;
            if(vis[i]) continue;
            while(1)
            {
                if(!vis[i])//若當前未訪問
                {
                    if(s)//加前括號
                    {
                        cout<<"("<<i;
                        s=false;
                        vis[i]=true;
                        i=a[i];//取指向的下一個數
                    }
                    else
                    {
                        vis[i]=true;
                        cout<<" "<<i;
                        i=a[i];
                    }
                }
                if(vis[i]) ++cnt;
                if(cnt==n)//當前括號內的全部訪問完畢
                {
                    if(e)
                    {
                        cout<<")";//加後括號
                        e=false;
                    }
                    break;
                }
            }
        }
        cout<<endl;
    }
    return 0;
}


相關文章