2017第十三屆湖南省省賽A - Seating Arrangement CSU - 1997
Mr. Teacher老師班上一共有n個同學,編號為1到n。 在上課的時候Mr. Teacher要求同學們從左至右按1,?2,?…,?n的順序坐成一排,這樣每個同學的位置是固定的,誰沒來上課就一目了然了。
但是時間長了之後,Mr. Teacher發現坐得離得遠的同學往往因為交流很少而逐漸變得生疏了,於是他決定重新安排同學們的座位,並且在新的座位安排中,任意兩個相鄰的同學的編號之差的絕對值都必須大於d。
現在Mr. Teacher需要你幫忙給出一個座位安排方案。
Input
輸入包含不超過100組數據。 每組數據包含兩個整數n,?d(4?≤?n?≤?100,?1?≤?d
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\n"); 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\n":"%d ", a[i]); } } int main() { input(); return 0; }
2017第十三屆湖南省省賽A - Seating Arrangement CSU - 1997