C. Diverse Permutation(Codeforces Round #275(div2)
1 second
256 megabytes
standard input
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 as nthe 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.
The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).
Print n integers forming the permutation. If there are multiple answers, print any of them.
3 2
1 3 2
3 1
1 2 3
5 2
1 3 2 4 5
By |x| we denote the absolute value of number x.
用n个数1~n,每一个数仅仅能用一次。组成差值的绝对值有k个数。为1~k。
输出任一个方案。
构造题,我是这样构造的,取前k+1个数。第一个数取1,先+k。后一个数-(k-1),在后一个数+k-2.......这样从两头往
中间靠拢。既取完了k+1个数。又构造了1~k的差值绝对值,至于k+1后的嘛,每次+1即可了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=100000+1000;
int ans[maxn];
int main()
{
int n,k;
ans[1]=1;
scanf("%d%d",&n,&k);
if(k==1)
{
for(int i=1;i<=n;i++)
ans[i]=i;
}
else
{
for(int i=2;i<=k+1;i++)
{
if(i%2)
ans[i]=ans[i-1]-(k-i+2);
else
ans[i]=ans[i-1]+(k-i+2);
}
int cur=1;
for(int i=k+2;i<=n;i++)
{
ans[i]=k+1+cur;
cur++;
}
}
for(int i=1;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
return 0;
}
C. Diverse Permutation(Codeforces Round #275(div2)的更多相关文章
- B. Friends and Presents(Codeforces Round #275(div2)
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...
- A. Counterexample (Codeforces Round #275(div2)
A. Counterexample time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
- 构造 Codeforces Round #275 (Div. 2) C. Diverse Permutation
题目传送门 /* 构造:首先先选好k个不同的值,从1到k,按要求把数字放好,其余的随便放.因为是绝对差值,从n开始一下一上, 这样保证不会超出边界并且以防其余的数相邻绝对值差>k */ /*** ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
随机推荐
- Java线程之基础
Java内存模型(jmm) 线程通信 消息传递 重排序 顺序一致性 Happens-Before As-If-Serial 一.线程的生命周期及五种基本状态 线程生命周期:新建.就绪.运行.阻塞.死亡 ...
- Base64编码字符串时数据量明显变大
那就是当把byte[]通过Convert.ToBase64String转换成Base64编码字符串时数据量明显变大 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需 ...
- 【mysql】新增、修改、删除、查询 语法讲义
一.DML - 数据操作语言 INSERT - 实现数据表数据的新增 UPDATE - 实现数据表数据的修改 DELETE - 实现数据表数据的删除 二.INSERT 语法: insert into ...
- VMware exsi 虚拟化嵌套
默认情况下exsi 虚拟化嵌套是没开启的 需要我们连接exsi主机,从后台找到对应的虚拟机修改配置文件开启虚拟化功能 1.连接exsi主机,开启ssh功能 2.ssh到exsi主机,修改配置文件 查找 ...
- worldcount
码云项目地址 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计划 20 20 Estimate 估计这个任务需要多少时间 60 70 Development 开发 ...
- caioj 1114 树形动态规划(TreeDP)3.0:多叉苹果树【scy改编ural1018二叉苹果树】
一波树上背包秒杀-- #include<cstdio> #include<cstring> #include<algorithm> #include<vect ...
- Fiddler 接口测试(Composer)的使用方法
原文:Fiddler 接口测试(Composer)的使用方法 下载地址:https://www.telerik.com/download/fiddler 一.Composer简介 右侧Composer ...
- Docker学习总结(8)——利用Docker开启持续交付之路
持续交付即Continuous Delivery,简称CD,随着DevOps的流行正越来越被传统企业所重视.持续交付讲求以短周期.小细粒度,自动化的方式频繁的交付软件,在这个过 程中要求开发.测试.用 ...
- What's the difference between returning void and returning a Task?
http://stackoverflow.com/questions/8043296/whats-the-difference-between-returning-void-and-returning ...
- poj--2385--Apple Catching(状态dp)
Apple Catching Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submi ...