日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C. Diverse Permutation(Codeforces Round #275(div2)

發布時間:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C. Diverse Permutation(Codeforces Round #275(div2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C. Diverse Permutation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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.

Input

The single line of the input contains two space-separated positive integers?n,?k?(1?≤?k?<?n?≤?105).

Output

Print?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 Note

By?|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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。