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 <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = ;
bool vis[N];
int res[N],a[N];
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
int j=i;
if(vis[j]) continue;
int id = ;
while(!vis[j]){
res[id++] = j;
vis[j]=true;
j = a[j];
}
printf("(");
for(int i=;i<id-;i++){
printf("%d ",res[i]);
}
printf("%d)",res[id-]);
}
printf("\n");
}
return ;
}

最新文章

  1. Tab切换
  2. MVVM架构~knockoutjs系列之包括区域级联列表的增删改
  3. HTTP协议简介1
  4. [转]Golang- import 导入包的语法
  5. 检测php网站是否已经被攻破
  6. Configuring Report Manager
  7. spring的配置模式与注解模式基础
  8. ASP缓存类收集
  9. mui 重写back 调用back方法,实现返回就即时刷新页面
  10. 类库探源——System.Math 和 Random
  11. PHP——四种基本排序算法
  12. Python - 定制pattern的string模板(template) 具体解释
  13. 关于使用X-UA-Compatible来设置IE浏览器兼容模式
  14. Percona XtraBackup的部分备份与恢复/单库备份/单表备份/指定库备份/指定表备份
  15. office2019下载以及激活密钥(亲测可用)
  16. TCP的三次握手 与 四次挥手
  17. Android dp、dip、dpi、px、sp简介及相关换算,及其应用实例
  18. Oracle中存储图片的类型为BLOB类型,Java中如何将其读取并转为字符串?
  19. 收藏的blog
  20. mysql普通查询日志

热门文章

  1. Python9-MySQL-MySQL存储过程-视图-触发器-函数-day45
  2. 第 8 章: 模块, 包与分发---Word
  3. HDU 3896 Greatest TC 双连通分量
  4. volley框架使用
  5. 解决maven项目Invalid bound statement (not found)的方法
  6. tomcat缓存
  7. 虚拟架构就绪 | 谈谈Windows Server 2012 R2迁移这件小事
  8. Python框架之Django学习笔记(十二)
  9. 1、IOS学习计划
  10. ogre3D学习基础17 --- 如何手动创建ogre程序