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 |p1p2||p1−p2|, |p2p3||p2−p3|, ..., |pn1pn||pn−1−pn|.

Input

Only one line contains two integers nn and kk.

1k<n1000001≤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 InputSample 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 }

 

转载于:https://www.cnblogs.com/weeping/p/6633299.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值