The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567

地址:http://acm.uestc.edu.cn/#/problem/show/1567

题目:

Jermutat1on

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

You are given two numbers nn and kk.

You are required to construct a permutation p1,p2,...,pnp1,p2,...,pn of numbers 1,2,...,n1,2,...,n such that there are exactly kk different numbers in |p1−p2||p1−p2|, |p2−p3||p2−p3|, ..., |pn−1−pn||pn−1−pn|.

Input

Only one line contains two integers nn and kk.

1≤k<n≤1000001≤k<n≤100000.

Output

Print nn integers forming the permutation.

If there are multiple answers, print any of them.

If there are no such permutation, print -1.

Sample input and output

Sample Input Sample Output
3 1
1 2 3

Source

The 15th UESTC Programming Contest Preliminary
 思路:水题,见代码。
 #include<iostream>
using namespace std; int n,k; int gcd(int x,int y)
{
if(y==)
return x;
else
return gcd(y,x%y);
} int main()
{
cin>>n>>k;
if(k>=n)
cout<<-<<endl;
else
{
if(k==)
for(int i=;i<=n;i++)
cout<<i<<' ';
else if(k%)
{
int mid=(k+)/;
cout<<mid<<' ';
for(int i=;i<=k;i++)
{
if(i%)
cout<<(mid+=i)<<' ';
else
cout<<(mid-=i)<<' ';
}
for(int i=mid+;i<=n;i++)
cout<<i<<' ';
}
else
{
int mid=k/+;
cout<<mid<<' ';
for(int i=;i<=k;i++)
{
if(i%)
cout<<(mid-=i)<<' ';
else
cout<<(mid+=i)<<' ';
}
for(int i=mid+;i<=n;i++)
cout<<i<<' ';
}
}
return ;
}
上一篇:linux 下安装 mysql 并配置 python 开发环境


下一篇:shell 加减乘除