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)
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
思路:水题,见代码。
1 #include<iostream>
2 using namespace std;
3
4 int n,k;
5
6 int gcd(int x,int y)
7 {
8 if(y==0)
9 return x;
10 else
11 return gcd(y,x%y);
12 }
13
14 int main()
15 {
16 cin>>n>>k;
17 if(k>=n)
18 cout<<-1<<endl;
19 else
20 {
21 if(k==1)
22 for(int i=1;i<=n;i++)
23 cout<<i<<' ';
24 else if(k%2)
25 {
26 int mid=(k+1)/2;
27 cout<<mid<<' ';
28 for(int i=1;i<=k;i++)
29 {
30 if(i%2)
31 cout<<(mid+=i)<<' ';
32 else
33 cout<<(mid-=i)<<' ';
34 }
35 for(int i=mid+1;i<=n;i++)
36 cout<<i<<' ';
37 }
38 else
39 {
40 int mid=k/2+1;
41 cout<<mid<<' ';
42 for(int i=1;i<=k;i++)
43 {
44 if(i%2)
45 cout<<(mid-=i)<<' ';
46 else
47 cout<<(mid+=i)<<' ';
48 }
49 for(int i=mid+1;i<=n;i++)
50 cout<<i<<' ';
51 }
52 }
53 return 0;
54 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。