CodeForces练习(3)

E. Two Round Dances

                                               time limit per test1 second
                                            memory limit per test256 megabytes
                                                  inputstandard input
                                                 outputstandard output

One day, n people (n is an even number) met on a plaza and made two round dances, each round dance consists of exactly n2 people. Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n2 people. Each person should belong to exactly one of these two round dances.

Round dance is a dance circle consisting of 1 or more people. Two round dances are indistinguishable (equal) if one can be transformed to another by choosing the first participant. For example, round dances [1,3,4,2], [4,2,1,3] and [2,1,3,4] are indistinguishable.

For example, if n=2 then the number of ways is 1: one round dance consists of the first person and the second one of the second person.

For example, if n=4 then the number of ways is 3. Possible options:

one round dance — [1,2], another — [3,4];
one round dance — [2,4], another — [3,1];
one round dance — [4,1], another — [3,2].
Your task is to find the number of ways n people can make two round dances if each round dance consists of exactly n2 people.

Input
The input contains one integer n (2≤n≤20), n is an even number.

Output
Print one integer — the number of ways to make two round dances. It is guaranteed that the answer fits in the 64-bit integer data type.

解法思路:
这道题困了我一会儿,想不出来该怎么写,看到这类题目我也有些头昏,去CSDN看了帖子,发现自己就是一个傻逼。。。。。。以后还是要多练习。。。。。。
废话不多说,思路真的很简单,这道题目的意思就是把地区用路连接起来,每个地区都属于一个“帮派”,要做的就是尽量用长度为一的路把所有的地区连通起来,注意同一帮派的地区不可以连。

那么我们可以先用数组存储这些地区,这里用a[n]来存储。
我们将第二个开始的每个地区和a[1]比较,如果他们属于同一个地区,就不连,如果属于不同的地区,就把他们连起来。同时用一个int类型的temp记录最后一个和a[1]不同的地区的下标。如果不满足,直接输出NO。
这样做的话,这一轮下来得到的结果就是,所有和第一个地区不同的地区都会和第一个地区联通,那剩下的和第一地区相同的地区,就和temp连接就好啦~~

另附代码链接:https://paste.ubuntu.com/p/C8DDfb6HCf/

posted @ 2020-10-29 00:39  原切  阅读(162)  评论(0)    收藏  举报