In the full arrangement of... n, at least K numbers are found in P1, P2, p3... Pn.
| P1-p2 |, | P2-P3 |,... | pn-1-PN | different elements!
Idea: ensure that the absolute value of the difference between two adjacent numbers is a monotonically decreasing sequence .....
If K is enough, add the unaccessed elements to the end!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 6 #define N 100005 7 using namespace std; 8 9 int vis[N];10 int num[N];11 int main(){12 int n, k;13 scanf("%d%d", &n, &k);14 num[1] = 1;15 vis[1] = 1;16 int c = k, i;17 for(i=2; i<=n; ++i){18 if(num[i-1]-c >0 && !vis[num[i-1]-c]){19 vis[num[i-1]-c]=1;20 num[i] = num[i-1]-c;21 }22 else if(num[i-1]+c <= n && !vis[num[i-1]+c]){23 vis[num[i-1]+c]=1;24 num[i] = num[i-1]+c;25 }26 --c;27 if(c < 1) break;28 }29 for(int j=1; j<=n; ++j)30 if(!vis[j]) num[++i]=j;31 printf("%d", num[1]);32 for(int j=2; j<=n; ++j)33 printf(" %d", num[j]);34 printf("\n");35 return 0;36 }View code
Codeforces C. diverse permutation (constructor)