博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4985(模拟)
阅读量:5154 次
发布时间:2019-06-13

本文共 2091 字,大约阅读时间需要 6 分钟。

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
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)
 
= = 把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

 

转载于:https://www.cnblogs.com/liyinggang/p/5631197.html

你可能感兴趣的文章
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
js兼容公用方法
查看>>
如何将应用完美迁移至Android P版本
查看>>
【转】清空mysql一个库中的所有表的数据
查看>>
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
Day03:Selenium,BeautifulSoup4
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>