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 knights, numbered from 1 to , participating in the tournament. The rules of the tournament are listed as follows:
The tournament consists of 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 rounds.
Let , , and , be four distinct integers. If
Knight fights against knight during round , and
Knight fights against knight during round , and
Knight fights against knight during round ,
then knight must fight against knight during round .
As DreamGrid’s general, you are asked to write a program to arrange all the duels in all the 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 , indicating the number of test cases. For each test case:
The first and only line contains two integers and (), indicating the number of knights participating in the tournament and the number of rounds.
It’s guaranteed that neither the sum of nor the sum of in all test cases will exceed 5000.
Output
For each test case:
If it’s possible to make a valid arrangement, output lines. On the -th line, output integers separated by one space, indicating that in the -th round, knight will fight against knight for all .
If there are multiple valid answers, output the lexicographically smallest answer.
Consider two answers and , let’s denote as the -th integer on the -th line in answer , and as the -th integer on the -th line in answer . Answer is lexicographically smaller than answer , if there exists two integers () and (), such that
for all and , , and
for all , , and finally .
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
題意
有 個騎士要參加 場比賽,每回合他們兩兩對決,對決過的兩個騎士之後不能再次對決。但是這裏有個要求:如果再某回合 對決 , 對決 ,那麼當某回合A對決 時, 要對決 。
如果能安排比賽,就輸出對決安排,否則就輸出Impossible
。
注意,輸出的對決安排需要是字典序最小的。
題解
這道題需要找規律。下面給出n爲8的表:
仔細觀察可以發現發生調換的區間寬度剛好是的:
而且發現每次調換都是區間內對稱的,這樣就不難寫出代碼了。
而一個 可以構造出的全部的組合是 ,如果所給的 大於這個值,就意味着這麼多的安排是不存在的。
代碼
#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;
}