• CSU 1997: Seating Arrangement【构造】


    1997: Seating Arrangement

    Description

    Mr. Teacher老师班上一共有n个同学,编号为1到n。 在上课的时候Mr. Teacher要求同学们从左至右按1, 2, …, n的顺序坐成一排,这样每个同学的位置是固定的,谁没来上课就一目了然了。

    但是时间长了之后,Mr. Teacher发现坐得离得远的同学往往因为交流很少而逐渐变得生疏了,于是他决定重新安排同学们的座位,并且在新的座位安排中,任意两个相邻的同学的编号之差的绝对值都必须大于d

    现在Mr. Teacher需要你帮忙给出一个座位安排方案。

    Input

    输入包含不超过100组数据。 每组数据包含两个整数n, d(4 ≤ n ≤ 100, 1 ≤ d ≤ n − 2)。

    Output

    对于每组数据,用一行输出一个可行的座位安排方案,相邻两个数之间用一个空格隔开。 座位安排方案由n个1到n的数组成,从左到右依次描述了各个座位安排给了哪个编号的同学。 如果有多种可行的座位安排方案,输出任意一种即可。 如果不存在满足要求的座位安排方案,则输出“-1”。

    Sample Input

    6 1
    6 3
    7 2

    Sample Output

    2 4 6 1 3 5
    -1
    1 4 7 3 6 2 5

    Hint

    对于第一个样例,存在多种可行的方案,如1 3 5 2 4 6,2 5 1 4 6 3,4 6 3 1 5 2等,输出任意一个可行方案即可。

    对于第三个样例,同样存在多种可行方案,输出任意一个可行方案即可。

    Source

    湖南省第十三届大学生计算机程序设计竞赛

    【代码】:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<map>
    #include<sstream>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<list>
    #include<vector>
    #include<string>
    
    using namespace std;
    #define  ll long long
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int inf = 0x3f3f3f3f;
    const int N = 1005;
    const int mod = 1000;
    int a[N];
    int n, m, d;
    int main()
    {
    
        while(cin>>n>>d)
        {
            int f = 1;
            if(d >= n/2){
                f = 0;
                puts("-1");
            }
            else{
                int i = 0, t = 0;
                while(i<n){
                    t++;
                    a[i++] = n/2+t;
                    a[i++] = t;
                }
            }
            if(f)
            for(int i = 0; i<n; i++){
                printf(i==n-1?"%d
    ":"%d ",a[i]);
            }
        }
        return 0;
    }
    // [n/2+1]  [1]  [n/2+2]  [2]  [n/2+3]  [3] ...
    中位数通常是构造中的关键
  • 相关阅读:
    javascript中无法通过div.style.left获取值的问题
    《Javascript高级程序设计第3版》精华总结
    前端面试笔试题回顾
    HTML 代码复用实践 (静态页面公共部分提取复用)
    将本地文件上传到远程库(二)
    ife-task0003学习收获总结
    将本地项目上传到git总结
    Minimum Size Subarray Sum —— LeetCode
    Binary Tree Preorder Traversal —— LeetCode
    Insertion Sort List —— LeetCode
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8719000.html
Copyright © 2020-2023  润新知