ZOJ4063 Tournament [The 2018 ACM-ICPC Asia Qingdao Regional Contest]

本文介绍了一个基于特定规则的骑士锦标赛调度算法,旨在为多个回合的比赛安排配对,确保每名骑士在不同回合与不同的对手进行对决,同时遵循一系列复杂的比赛规则。
Tournament

Time Limit: 1 Second      Memory Limit: 65536 KB

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

 找规律打表预处理。

#include <bits/stdc++.h>
using namespace std;
int T;
int n,m;
int a[1100][1100];
int ans[1100],tol;

void cal(){
    a[1][1] = 1;
    for (int i = 2;i <= 1024;i *= 2){
        int k = i >> 1;
        for (int r = 1;r <= k;++r){
            for (int c = 1;c <= k;++c){
                a[r][c+k] = a[r][c] + k;
                a[r+k][c] = a[r][c] + k;
                a[r+k][c+k] = a[r][c];
            }
        }
    }
}

int main(){
    
    cal();
    
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        tol = 0;
        for (int i = 2;i <= n;++i){
            int flag = 0;
            for (int j = 1;j <= n;++j) {
                if (a[i][j] > n) {
                    flag = 1;
                    break;
                }
            }
            if (!flag) ans[tol++] = i;
        }
        if (tol < m) {
            puts("Impossible");continue;
        }
        for (int i = 0;i < m;++i){
            for (int j = 1;j <= n;++j){
                printf("%d%c",a[ans[i]][j]," \n"[j==n]);
            }
        }
    }
    return 0;
} 

 

转载于:https://www.cnblogs.com/mizersy/p/9909714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值