CF#275 (Div. 2) C(數學構造)
阿新 • • 發佈:2018-12-24
C. Diverse Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
input
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers not larger than n. We'll denote asn the 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|, ..., |p n - 1 - pn| has
exactly k distinct elements.
The 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) input3 2output
1 3 2
3 1output
1 2 3input
5 2output
1 3 2 4 5Note
By |x| we denote the absolute value of number x.
解題思路: 題目大意是說要求構造一個n個數的排列,要求相鄰兩數的差值的絕對值保證是k個不同的數。 再次被神奇的數學所折服·····我們先構造1~n-k這些數,然後構造2~n這些數。 完整程式碼:#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL; /** Constant List .. **/ //{ const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI; int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n , k; while(~scanf("%d%d",&n,&k)) { int tmp = n; int key = n - k; for(int i = 1 ; i <= key ; i ++) printf("%d ", i ); key++; while(key <= tmp) { printf("%d " , tmp--); if(key <= tmp) printf("%d ",key ++); else break; } printf("\n"); } }