1 /* 2 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放。因为是绝对差值,从n开始一下一上, 3 这样保证不会超出边界并且以防其余的数相邻绝对值差>k 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-2 9:20:01 8 File Name :B.cpp 9 *************************************************/ 10 11 #include <cstdio> 12 #include <algorithm> 13 #include <iostream> 14 #include <sstream> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <vector> 19 #include <queue> 20 #include <deque> 21 #include <stack> 22 #include <list> 23 #include <map> 24 #include <set> 25 #include <bitset> 26 #include <cstdlib> 27 #include <ctime> 28 using namespace std; 29 30 typedef long long ll; 31 const int MAXN = 1e5 + 10; 32 const int INF = 0x3f3f3f3f; 33 const int MOD = 1e9 + 7; 34 bool vis[MAXN]; 35 int a[MAXN]; 36 37 int main(void) { //CodeForces 483C Diverse Permutation 38 int n, k; 39 while (scanf ("%d%d", &n, &k) == 2) { 40 if (k == 1) { 41 for (int i=1; i<=n; ++i) { 42 printf ("%d%c", i, (i==n) ? ‘\n‘ : ‘ ‘); 43 } 44 continue; 45 } 46 memset (vis, false, sizeof (vis)); 47 int j = 1; a[0] = n; vis[a[0]] = true; bool flag = false; 48 for (int i=k; i>=1; --i) { 49 if (flag) { 50 a[j] = a[j-1] + i; vis[a[j]] = true; ++j; flag = !flag; 51 } 52 else { 53 a[j] = a[j-1] - i; vis[a[j]] = true; j++; flag = !flag; 54 } 55 } 56 int p = n; 57 for (int i=j; i<n; ++i) { 58 for (int k=p; k>=1; --k) { 59 if (!vis[k]) { 60 a[i] = k; vis[k] = true; p = k; break; 61 } 62 } 63 } 64 for (int i=0; i<n; ++i) { 65 printf ("%d%c", a[i], (i==n-1) ? ‘\n‘ : ‘ ‘); 66 } 67 } 68 69 return 0; 70 }
时间: 2024-11-02 17:48:18