https://codeforces.com/contest/1711/problem/A

A. Perfect Permutation

You are given a positive integer n.

The weight of a permutaion p1,p2,...,pn is the number of indices(索引) 1<=i<=n such that i divides pi. Find a permutation p1,p2...pn with the minimum possible weight(among all permutation of length n)

A permutaion is an array consisting of n distinct integers from 1 to n in arbitrary order.

For example,[2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4]is also not a permutation (n=3 but there is 4 in the array).

Output

For each test case, print a line containing n integers p1,p2,…,pn so that the permutation p has the minimum possible weight.

If there are several possible answers, you can print any of them.

Example

2

1

4

Output

1

2 1 4 3

Note

In the first test case, the only valid permutation is p=[1]. Its weight is 1.

In the second test case, one possible answer is the permutation p=[2,1,4,3]. One can check that 1 divides p1 and i does not divide pi for i=2,3,4,

 so the weight of this permutation is 1.

It is impossible to find a permutation of length 4 with a strictly smaller weight.

大概题意:例如4,寻找4以内的不同正数即可。可以是4 1 2 3 这样的。只要不同,并且最大数<nJ即可。

这里不是很好理解。先存这里,以后再仔细阅读。(代码参考别人的)

以后遇到类似的题,可以这样做。

#include <iostream>
#include <vector>
using namespace std;

int main() 
{
	int t = 0, n=0;
	cin >> t;
	char space1 = ' ';
	while (t--)
	{
		cin >> n;

		cout << n << space1;
		for (int i = 1; i<n; i++)
		{
			cout << i << space1;
		}
		cout << endl;
	}
	return 0;
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐