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 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|, ..., |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个数的排列,要求相邻两数的差值的绝对值保证是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");
}
}