2018 ACM-ICPC 亚洲区域赛青岛现场赛 —— Problem F. Tournament

题面:http://acm.zju.edu.cn/contest-materials/qd2018/qd2018_problems.pdf

题意:

n个骑士决斗K轮

要求是每个骑士只能跟另外一个骑士决斗一次

每轮必须有 n/2 场决斗

如果在某轮A和B单挑,C和D单挑

那么在下面的论场中必然有A和C单挑同时B和D单挑

思路:

用一个set存每个骑士还没单挑过的其他骑士编号,一个set存每轮还没有单挑过的骑士

先预处理第一轮的21436587······

第一个骑士每轮单挑必然是单挑的轮数+1(如第一轮单挑的是2即1+1)

这样才能满足字典序最小的要求

然后把第一个骑士单挑的第X个骑士的上一个和第一个的上一个进行单挑

然后找本轮还没单挑的编号最小的骑士去单挑本轮还没单挑的他还没单挑过的最小的(好拗口啊ORZ)

代码上能看的比较清楚,建议直接分析代码

另外如果k>=lowbit(n) 那么绝对不可能安排上

具体可以自己打表看看

 

代码(注意这不是最终代码):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define it iterator
#define ll long long
#define eb emplace_back
#define lowbit(x) x & -x
#define all(x) x.begin(),x.end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define per(x,a,b) for(int x = a; x <= b; x++)
#define rep(x,a,b) for(int x = a; x >= b; x--)
#define IO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)

const int birth = 19260817;
const int mo = 998244353;
const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3fffffff;
const double eps = 1e-8;

int ch[maxn][maxn];

//******************THE PROGRAM BEGINING******************

int main()
{
    //cout << (lowbit(12)) << endl;
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        set<int> s[maxn];
        set<int> e;
        cin >> n >> k;

        if (k >= (lowbit(n)))
        {
            cout << "Impossible" << endl;
            continue;
        }

        if (lowbit(n) == 2 || k == 1)
        {
            per(i, 1, n)
            {
                if (i & 1)
                    cout << i + 1;
                else
                    cout << i - 1;
                if (i != n)
                    cout << " ";
            }
            cout << endl;
            continue;
        }
        else
        {
            per(i, 1, n)
            {
                if (i & 1)
                    ch[0][i] = i + 1;
                else
                {
                    ch[0][i] = i - 1;
                }
                per(j, 1, n)
                {
                    if (i == j || j == ch[0][i])
                        continue;
                    s[i].insert(j);
                }
            }
            int x = k - 1;
            int num = 3;
            int pos = 1;
            while (x--)
            {
                e.clear();
                per(i, 2, n)
                    e.insert(i);
                e.erase(num);
                ch[pos][1] = num;
                ch[pos][num] = 1;
                s[num].erase(1);
                ch[pos][ch[pos - 1][1]] = ch[pos - 1][num];
                e.erase(ch[pos - 1][num]);
                ch[pos][ch[pos - 1][num]] = ch[pos - 1][1];
                e.erase(ch[pos - 1][1]);
                s[ch[pos - 1][1]].erase(ch[pos - 1][num]);
                s[ch[pos - 1][num]].erase(ch[pos - 1][1]);
                while (!e.empty())
                {
                    int head = *e.begin();
                    int tail;
                    e.erase(head);
                    for (set<int>::iterator it = s[head].begin(); it != s[head].end(); it++)
                    {
                        if (e.count(*it))
                        {
                            tail = *it;
                            break;
                        }
                    }

                    e.erase(tail);
                    ch[pos][head] = tail;
                    ch[pos][tail] = head;
                    s[head].erase(tail);
                    s[tail].erase(head);
                    ch[pos][ch[pos - 1][head]] = ch[pos - 1][tail];
                    e.erase(ch[pos - 1][head]);
                    ch[pos][ch[pos - 1][tail]] = ch[pos - 1][head];
                    e.erase(ch[pos - 1][tail]);
                    s[ch[pos - 1][tail]].erase(ch[pos - 1][head]);
                    s[ch[pos - 1][head]].erase(ch[pos - 1][tail]);
                }
                num++;
                pos++;
            }

            for (int i = 0; i < k; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    cout << ch[i][j];
                    if (j != n)
                        cout << " ";
                }
                cout << endl;
            }
        }
    }

    return 0;
}

 

但是呢,这个方法从时间上讲完全是不够用的,然后通过上面的进行打表,发现了一个天大的咪咪

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int ch[1010][1010];

//******************THE PROGRAM BEGINING******************

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        scanf("%d %d", &n, &k);

        if (k >= (n & -n))
        {
            puts("Impossible");
            continue;
        }

        if ((n & -n) == 2 || k == 1)
        {
            for(int i = 1; i <= n; i++)
            {
                if (i & 1)
                    printf("%d", i + 1);
                else
                    printf("%d", i - 1);
                if (i != n)
                    printf(" ");
            }
            printf("\n");
            continue;
        }
        else
        {
            for (int i = 1; i <= n; i++)
            {
                if (i & 1)
                {
                    ch[0][i] = i;
                    ch[1][i] = i + 1;
                }
                else
                {
                    ch[0][i] = i;
                    ch[1][i] = i - 1;
                }
            }

            int x = 2;
            while (x <= k)
            {
                for (int i = x; i <= x * 2 - 1; i++)
                {
                    for (int j = 1; j <= n; j += 2 * x)
                    {
                        for (int k = j; k < j + x; k++)
                        {
                            ch[i][k] = ch[i - x][k + x];
                        }
                        for (int k = j + x; k < j + 2 * x; k++)
                        {
                            ch[i][k] = ch[i - x][k - x];
                        }
                    }
                }
                x *= 2;
            }

            for (int i = 1; i <= k; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    printf("%d", ch[i][j]);
                    if (j != n)
                        printf(" ");
                }
                printf("\n");
            }
        }
    }

    return 0;
}

 

其实就是定长翻倍交换左右块就好了

先打表前两行

第三第四行为2*2的块交换

第五到第八行为4*4的块交换

依次推下去即可

转载于:https://www.cnblogs.com/qq965921539/p/9905086.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值