hdu 4985(模拟)
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

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).
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).
2 5 4 3 1
3
1 2 3
(1)(2)(3)
#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 ;
}
hdu 4985(模拟)的更多相关文章
- hdu 4891 模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...
- hdu 5012 模拟+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...
- BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...
- hdu 4669 模拟
思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...
- 2013杭州网络赛C题HDU 4640(模拟)
The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU/5499/模拟
题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...
- hdu 5003 模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=5003 记得排序后输出 #include <cstdio> #include <cstring& ...
- hdu 5033 模拟+单调优化
http://acm.hdu.edu.cn/showproblem.php?pid=5033 平面上有n个建筑,每个建筑由(xi,hi)表示,m组询问在某一个点能看到天空的视角范围大小. 维护一个凸包 ...
- HDU 2860 (模拟+并查集)
Regroup Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
随机推荐
- Java课堂作业详解
今天的Java课堂留下了一个作业:使用Eclipse编写一个程序,使输入的两个数进行加和,并且输出他们的和.对于这个题目,我们首先可以把它分解成为三个不同的小步骤 第一步就是输入这两个数,因为我们无需 ...
- 牛客练习赛42 A 字符串
题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...
- 其它- in-place
in-place 刷编程题的时候,经常遇到题目要求do in-place.所以就上网搜了相关概念,简单总结一下. in-place操作,意思是所有的操作都是”就地“操作,不允许进行移动,或者称作 ...
- Alarm机制用于定时服务
获取一个 AlarmManager 的实例: AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE) ...
- CQRS之旅——旅程5(准备发布V1版本)
旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...
- android adb常用指令
介绍一个更详细的介绍ADB的: https://github.com/mzlogin/awesome-adb/blob/master/README.md ----------------------- ...
- 踩坑 Uncaught RangeError: Maximum call stack size exceeded
今天遇到了一个错误, 堆栈溢出,很好奇就是一个简单ajax请求怎么会报这个错误,研究了一下,发现犯了一个很低级的错误,data的参数错误了: passWord是未定义的变量,值为空,然后导致了这个问题 ...
- Leetcode 558.四叉树交集
四叉树交集 四叉树是一种树数据,其中每个结点恰好有四个子结点:topLeft.topRight.bottomLeft 和 bottomRight.四叉树通常被用来划分一个二维空间,递归地将其细分为四个 ...
- ServletActionContext 源码
/* * $Id: ServletActionContext.java 651946 2008-04-27 13:41:38Z apetrelli $ * * Licensed to the Apac ...
- js确保正确this的几种写法
1.直接用bind调用 this.method.bind(this) 2.构造函数中用bind定义 class Foo{ constructor(){ this.method = this.metho ...