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() {
	while (~scanf("%d", &n)) {
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]);
		for (int i = 1; i <= n; i++) {
			if (vis[i]) continue;
			int t = i;
			printf("(%d", t);
			vis[t] = 1;
			t = a[t];
			while (vis[t] == 0) {
				vis[t] = 1;
				printf(" %d", t);
				t = a[t];
			}
			printf(")");
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-08 07:16:26

HDU 4985 Little Pony and Permutation(置换)的相关文章

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 #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

【HDOJ】4985 Little Pony and Permutation

水题. 1 #include <cstdio> 2 3 #define MAXN 100005 4 5 int buf[MAXN], n; 6 7 int main() { 8 int i, j, k; 9 10 while (scanf("%d", &n) != EOF) { 11 for (i=1; i<=n; ++i) 12 scanf("%d", &buf[i]); 13 for (i=1; i<=n; ++i) {

HDU 4986 Little Pony and Alohomora Part I(递推+犹拉常数)

HDU 4986 Little Pony and Alohomora Part I 题目链接 题意:一些钥匙随机放在箱子里,现在问打开次数期望 思路:每种方式相当于一个置换的循环个数,那么考虑f[i]为i个箱子的情况,f[i + 1]要么就是放在最后多一个循环,要么就是插入中间循环个数不变,对应的转移为f[i + 1] = (f[i] + 1) / i + f[i] * (i - 1) / i 化简得到f[i] = f[i - 1] + 1 / i 这个式子i越大,越趋近lni + C,这个C为

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 Problem Description As a unicorn, the ability of using magic is the distinguishing

BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每个循环包含的数字. 还是不太懂吧?反正比赛的时候我也没看懂 >__< ! 据说,这条题目用到了置换群 + 循环 的知识,黑书上 P246 有相应介绍. 先来说明下这幅图的意思,搞明白题意就发现这条题是很好做的. 它表示:σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3,

BestCoder Round #7

A(hdu4985) - Little Pony and Permutation 1 #include <cstdio> 2 3 using namespace std; 4 5 #define NN 100010 6 int g[NN], f[NN]; 7 8 int main(void) 9 { 10 int N; 11 while(scanf("%d", &N) > 0) { 12 for(int i=1; i<=N; ++i) scanf(&qu

BFS学习总结

BFS学习总结 给你一个n*m的网格迷宫,迷宫中有些格子不能走,其他的格子都能走.然后给你起点与终点,问你从起点走到终点最少需要多少步? 上面的问题就是一个典型的BFS问题,对于这类问题来说,只要你掌握了这类问题的关键思想,其实他们都是可以用类似的思路来做的. 你可以把BFS问题想象成:从一个父亲(起点状态)生儿子(后继状态),儿子又生孙子(后继状态)的过程,只要这个家族中出生了一个满意的后代(终点状态),这个家族就不生了. 但是如果这个家族中有两个完全一样的人出生(他们的辈分不一定相同),那么

BestCoder Round #7-A,B,C

A:Little Pony and Permutation 直接暴力搜索,复杂度O(n) #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> #include<vector> #include<math.h> #include<queue> #include<stack