ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4063

Input

Output

Sample Input

2
3 1
4 3

Sample Output

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

题意:

说现在有 $n$ 个人打比赛,要你安排 $k$ 轮比赛,要求每轮每个人都参加一场比赛。

所有轮次合起来,任意一对人最多比赛一场。

且对于任意的第 $i,j$ 轮,若 $a,b$ 在第 $i$ 轮进行了一场比赛,$c,d$ 在第 $i$ 进行了一场比赛,则 $a,c$ 在第 $j$ 轮必须进行一场比赛,$b,d$ 在第 $j$ 必须进行一场比赛。

现在要你安排比赛。

题解:

算法教材上有过一道题叫“循环赛日程表”:

把对于题目所给的 $n$,找到一个最小的 $m$ 满足 $2^m \ge n$,然后按照上面的思路递归打出表格,

然后从第二行开始进行打印前 $n$ 列,如果接下来的 $k$ 行中所有的数都是在 $1 \sim n$ 里的,说明是OK的。否则就做不到比赛 $k$ 轮。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,k;
int mp[1030][1030];
void t(int x)
{
    if(x==2)
    {
        mp[1][1]=1, mp[1][2]=2;
        mp[2][1]=2, mp[2][2]=1;
        return;
    }
    t(x/2);
    for(int i=1;i<=x/2;i++)
    {
        for(int j=1;j<=x/2;j++)
        {
            mp[i][j+x/2]=mp[i][j]+x/2;
            mp[i+x/2][j+x/2]=mp[i][j];
            mp[i+x/2][j]=mp[i][j]+x/2;
        }
    }
}
bool ok()
{
    for(int i=2;i<=k+1;i++) {
        for(int j=1;j<=n;j++) {
            if(mp[i][j]>n) return 0;
        }
    }
    return 1;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&k);
        if(n%2 || k>=n)
        {
            printf("Impossible\n");
            continue;
        }

        int N=1024;
        while((N/2)>=n) N/=2;

        t(N);

        if(!ok())
        {
            printf("Impossible\n");
            continue;
        }
        else
        {
            for(int i=2;i<=k+1;i++) {
                for(int j=1;j<=n;j++) {
                    printf("%d%c",mp[i][j],j==n?‘\n‘:‘ ‘);
                }
            }
        }
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9949487.html

时间: 2024-08-29 22:39:42

ZOJ 4063 - Tournament - [递归][2018 ACM-ICPC Asia Qingdao Regional Problem F]的相关文章

ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ 个植物,编号为 $1 \sim n$,第 $i$ 个植物的位置在坐标 $i$,成长值为 $a_i$,初始防御值为 $d_i$. 现在有一辆小车从坐标 $0$ 出发,每次浇水操作必须是先走 $1$ 单位长度,然后再进行浇水,植物被浇一次水,防御值 $d_i+=a_i$. 现在知道,小车最多进行 $m

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 735    Accepted Submission(s): 305 Problem Description Bob has N balls and A b

hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup

http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Alice was felling into a cave. She found a strange door with a number square m

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que