codechef Permutation Cycles 题解

We consider permutations of the numbers 1,..., N

for some N. By permutation we mean a rearrangment of the

number 1,...,N. For example

2  4  5  1  7  6  3  8

is a permutation of 1,2,...,8. Of course,

1  2  3  4  5  6  7  8

is also a permutation of 1,2,...,8.

We can "walk around" a permutation in a interesting way and here

is how it is done for the permutation above:

Start at position 1. At position 1 we have 2 and so we go to

position 2. Here we find 4 and so we go to position 4. Here we find

1, which is a position that we have already visited. This completes

the first part of our walk and we denote this walk by (1 2 4 1). Such

a walk is called a cycle. An interesting property of such

walks, that you may take for granted, is that the position we revisit

will always be the one we started from!

Examples

Sample input 1:

8

2 4 5 1 7 6 3 8

Sample output 1:

4

1 2 4 1

3 5 7 3

6 6

8 8

Sample input 2:

8

1 2 3 4 5 6 7 8

Sample output 2:

8

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

原来permutation有这样的cycle的,很好玩。

下面程序带了个自制的输入输出int的处理。使用getc, putchar这些输入输出的确会快很多的。

#pragma once
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <iostream>
using namespace std;

namespace
{

int scanInt2()
{
	char c = getc(stdin);
	while (c < ‘0‘ || c > ‘9‘)
	{
		c = getc(stdin);
	}
	int num = 0;
	while (‘0‘ <= c && c <= ‘9‘)
	{
		num = (num<<3) + (num<<1) + c - ‘0‘;
		c = getc(stdin);
	}
	return num;
}

void printInt(int n)
{
	int i = 0;
	char buffer[32] = {0};
	while (n)
	{
		buffer[i++] = n % 10 + ‘0‘;
		n /= 10;
	}
	//buffer[i] = ‘\0‘;
	//fputs(buffer, stdout);
	for (i--; i >= 0; i--)
	{
		fputc(buffer[i], stdout);
	}
}

}

int PermutaionCycles()
{
	const int n = scanInt2();
	int *A = new int[n+1];
	for (int i = 1; i <= n; i++)
	{
		A[i] = scanInt2();
	}
	vector<vector<int> > rs;
	vector<int> tmp;
	for (int i = 1; i <= n; i++)
	{
		if (A[i] != -1)
		{
			tmp.clear();
			int j = i;
			while (tmp.size() < 2 || tmp.back() != i)
			{
				tmp.push_back(j);
				int t = j;
				j = A[j];
				A[t] = -1;
			}
			rs.push_back(tmp);
		}
	}
	printInt(rs.size());
	putc(‘\n‘, stdout);
	for (int i = 0; i < (int)rs.size(); i++)
	{
		for (int j = 0; j < int(rs[i].size()); j++)
		{
			printInt(rs[i][j]);
			putc(‘ ‘, stdout);
		}
		putchar(‘\n‘);
	}
	delete [] A;
	return 0;
}

codechef Permutation Cycles 题解,布布扣,bubuko.com

时间: 2024-08-05 15:16:01

codechef Permutation Cycles 题解的相关文章

Codechef Nuclear Reactors 题解

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will cause 1 particl

codechef Cleaning Up 题解

After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have already left and onl

codechef Turbo Sort 题解

Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7 大数据的排序,输入和输出. 一开始使用了cou

LeetCode: Permutation Sequcence 题解

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" "3

codechef Top Batsmen题解

A cricket team consists of 11 players and some are good at batting, others are good at bowling and some of them are good at both batting and bowling. The batting coach wants to select exactly K players having maximum possible sum of scores. Given the

codechef Birthday Candles 题解

Birthday Candles The chef is preparing a birthday cake for one of his guests, and his decided to write the age of the guest in candles on the cake. There are 10 types of candles, one for each of the digits '0' through '9'. The chef has forgotten the

codechef Cutting Recipes题解

Cutting Recipes The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredie

codechef Prime Palindromes 题解

给定一个数,求一个新数要大于等于这个数,而这个新数既要是palindromes回文又要是prime素数. 题目很简单,有人都使用取巧的方法保存好结果直接查表. 或者暴力法求解. 这里不使用保存表的方法,也不要用暴力法.- 这些方法都不好. 使用的技巧有: 1 而是使用next palindrome的技巧,只需要O(n),n是数位,可以认为是常数了. 2 判断素数的方法,时间效率是O(sqrt(n)), n是数值大小,如果是重复判断很多数是否是素数是有办法优化的,但是如果是单个素数判断的话,我还想

codechef Recipe Reconstruction 题解

Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ing