zoukankan      html  css  js  c++  java
  • 2017第十三届湖南省省赛A

    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等,输出任意一个可行方案即可。

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

    补题来了,这道题在比赛的时候其实就是已经有思路的,只不过当时整个人处于一个懵的状态没有写出来。ε=(´ο`*)))唉!!!

    思路:

    首先,我们可以很直观的就想到输出-1的情况就是d>=n/2。

    然后我们就可以想到对于任何数n最大的d,其实就是n/2-1。也就是说我将1 2 3 4 .... n从n/2处分开将前面的1 2 ... n/2 插入到 n/2+1 ... n中间得到的序列 n/2+1 1 n/2+2 2 .. n n/2 就是d最大的情况,只要有序列,那么这个序列一定是满足条件的序列。

    // Asimple
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <stack>
    #include <set>
    #include <map>
    #include <cmath>
    #define INF 0x3f3f3f3f
    #define debug(a) cout<<#a<<" = "<<a<<endl
    #define test() cout<<"============"<<endl
    #define CLS(a,v) memset(a, v, sizeof(a))
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 105;
    const ll mod = 1000000007;
    int n, m, T, len, cnt, num, ans, Max, k; 
    int d;int a[maxn];
    
    void input(){
        while( cin >> n >> d ) {
            if( d >= n/2 ) {
                printf("-1
    ");
                continue;
            }
            CLS(a, 0);
            k = 0;
            int t = 0;
            while( k<n ) {
                t ++;
                a[k++] = n/2+t;
                a[k++] = t;
            }
            for(int i=0; i<n; i++) printf(i==n-1?"%d
    ":"%d ", a[i]);
        }
    }
    
    int main() {
        input();
        return 0;
    } 
  • 相关阅读:
    [RxJS] Filtering operators: throttle and throttleTime
    [RxJS] Transformation operators: debounce and debounceTime
    [AngularJS] Default Child state and nav between child state
    [RxJS] Transformation operator: bufferToggle, bufferWhen
    [RxJS] Transformation operators: delay and delayWhen
    [RxJS] Transformation operator: buffer, bufferCount, bufferTime
    [RxJS] Transformation operator: scan
    [置顶] 利用Global.asax的Application_Error实现错误记录,错误日志
    Java方法重写与方法重载
    Inside GDALAllRegister之一: 五大部分
  • 原文地址:https://www.cnblogs.com/Asimple/p/7500346.html
Copyright © 2011-2022 走看看