F - Tournament(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

F - Tournament(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

Time limit:1000 ms
Memory limit:65536 kB
judge:
ZOJ - 4063
vjudge

Description

DreamGrid, the king of Gridland, is making a knight tournament. There are nn knights, numbered from 1 to nn, participating in the tournament. The rules of the tournament are listed as follows:

The tournament consists of kk rounds. Each round consists of several duels. Each duel happens between exactly two knights.
Each knight must participate in exactly one duel during each round.
For each pair of knights, there can be at most one duel between them during all the kk rounds.
Let 1i,jk1 \le i, j \le k, iji \ne j, and 1a,b,c,dn1 \le a, b, c, d \le n, a,b,c,da, b, c, d be four distinct integers. If
Knight aa fights against knight bb during round ii, and
Knight cc fights against knight dd during round ii, and
Knight aa fights against knight cc during round jj,
then knight bb must fight against knight dd during round jj.
As DreamGrid’s general, you are asked to write a program to arrange all the duels in all the kk rounds, so that the resulting arrangement satisfies the rules above.

Input

There are multiple test cases. The first line of the input is an integer TT, indicating the number of test cases. For each test case:

The first and only line contains two integers nn and kk (1n,k10001 \le n, k \le 1000), indicating the number of knights participating in the tournament and the number of rounds.

It’s guaranteed that neither the sum of nn nor the sum of kk in all test cases will exceed 5000.

Output

For each test case:

If it’s possible to make a valid arrangement, output kk lines. On the ii-th line, output nn integers ci,1,ci,2,,ci,nc_{i, 1}, c_{i, 2}, \dots, c_{i, n} separated by one space, indicating that in the ii-th round, knight jj will fight against knight ci,jc_{i, j} for all 1jn1 \le j \le n.

If there are multiple valid answers, output the lexicographically smallest answer.

Consider two answers AA and BB, let’s denote ai,ja_{i, j} as the jj-th integer on the ii-th line in answer AA, and bi,jb_{i, j} as the jj-th integer on the ii-th line in answer BB. Answer AA is lexicographically smaller than answer BB, if there exists two integers pp (1pk1 \le p \le k) and qq (1qn1 \le q \le n), such that

for all 1i<p1 \le i < p and 1jn1 \le j \le n, ai,j=bi,ja_{i, j} = b_{i, j}, and
for all 1j<q1 \le j < q, ap,j=bp,ja_{p, j} = b_{p, j}, and finally ap,q<bp,qa_{p, q} < b_{p, q}.
If it’s impossible to make a valid arrangement, output “Impossible” (without quotes) in one line.
Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input

2
3 1
4 3

Sample Output

Impossible
2 1 4 3
3 4 1 2
4 3 2 1

题意

nn 个骑士要参加 kk 场比赛,每回合他们两两对决,对决过的两个骑士之后不能再次对决。但是这里有个要求:如果再某回合 AA 对决 BBCC 对决 DD ,那么当某回合A对决 CDC(D) 时, BB 要对决 DCD(C)

如果能安排比赛,就输出对决安排,否则就输出Impossible

注意,输出的对决安排需要是字典序最小的。

题解

这道题需要找规律。下面给出n为8的表:

在这里插入图片描述
仔细观察可以发现发生调换的区间宽度刚好是iilowbit(i)2lowbit(i) * 2
在这里插入图片描述
而且发现每次调换都是区间内对称的,这样就不难写出代码了。

而一个 nn 可以构造出的全部的组合是 lowbit(n)1lowbit(n)-1 ,如果所给的 kk 大于这个值,就意味着这么多的安排是不存在的。

代码

#include <bits/stdc++.h>
#define maxn 1005
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define sc(x) scanf("%d", &x)
#define lowerbit(x) (x & (-x))
using namespace std;

int T, n, k, a[maxn];

void sol() {
	if (k > lowerbit(n) - 1) {
		printf("Impossible\n");
		return;
	}
	_rep(i, 1, n) a[i] = i;
	for (int i = 1; i <= k; ++i) {
		int len = lowerbit(i);
		for (int j = 1; j <= n; j += (len << 1)) {
			_for(l, len) {
				swap(a[j + l], a[j + len * 2 - 1 - l]);
			}
		}
		_rep(j, 1, n) printf("%d%s", a[j], j == n ? "\n" : " ");
	}
}

int main() {
	while (cin >> T) {
		_for(i, T) {
			sc(n), sc(k);
			sol();
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章