C. Diverse Permutation(Codeforces Round #275(div2)
Permutation?p?is an ordered set of integers?p1,???p2,???...,???pn, consisting of?n?distinct positive integers not larger than?n. We'll denote as?nthe length of permutation?p1,???p2,???...,???pn.
Your task is to find such permutation?p?of length?n, that the group of numbers?|p1?-?p2|,?|p2?-?p3|,?...,?|pn?-?1?-?pn|?has exactly?k?distinct elements.
InputThe single line of the input contains two space-separated positive integers?n,?k?(1?≤?k?<?n?≤?105).
OutputPrint?n?integers forming the permutation. If there are multiple answers, print any of them.
Sample test(s) input 3 2 output 1 3 2 input 3 1 output 1 2 3 input 5 2 output 1 3 2 4 5 NoteBy?|x|?we denote the absolute value of number?x.
用n個數1~n,每一個數僅僅能用一次。組成差值的絕對值有k個數。為1~k。
輸出任一個方案。
構造題,我是這樣構造的,取前k+1個數。第一個數取1,先+k。后一個數-(k-1),在后一個數+k-2.......這樣從兩頭往
中間靠攏。既取完了k+1個數。又構造了1~k的差值絕對值,至于k+1后的嘛,每次+1即可了。
代碼:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=100000+1000; int ans[maxn]; int main() {int n,k;ans[1]=1;scanf("%d%d",&n,&k);if(k==1){for(int i=1;i<=n;i++)ans[i]=i;}else{for(int i=2;i<=k+1;i++){if(i%2)ans[i]=ans[i-1]-(k-i+2);elseans[i]=ans[i-1]+(k-i+2);}int cur=1;for(int i=k+2;i<=n;i++){ans[i]=k+1+cur;cur++;}}for(int i=1;i<n;i++)printf("%d ",ans[i]);printf("%d\n",ans[n]);return 0; }
總結
以上是生活随笔為你收集整理的C. Diverse Permutation(Codeforces Round #275(div2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 表单防重复提交
- 下一篇: Python 的时间格式化