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\)的矩阵可 ...
随机推荐
- BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)
BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...
- 解决Vue引入百度地图JSSDK:BMap is undefined 问题
百度地图官网文档介绍使用JSSDK时,仅提供了2种引入方式: script引入 异步加载 解决跨域问题,实例调用百度地图 但vue项目中仅某一两个页面需要用到百度地图,所以不想在 index.html ...
- 洛谷 P2818 天使的起誓
P2818 天使的起誓 题目描述 Tenshi非常幸运地被选为掌管智慧之匙的天使.在正式任职之前,她必须和其他新当选的天使一样要宣誓.宣誓仪式是每位天使各自表述自己的使命,他们的发言稿放在n个呈圆形排 ...
- scratchIDE使用说明
scratchIDE使用说明
- cocos2dx 几个精灵依照顺序播放动画解决方法
我先描写叙述一下这个问题: 拿之前做的卡牌游戏来说.假设一方场上有3张牌,那么肯定要以一种顺序来播放攻击动画.我是以从左到右的方式. 我的解决方案是向每张牌都传递一个延时參数,然后在runAction ...
- jmeter名词解释之时间(Elapsed Time/ Latency Time/Connection Time)
转载时请标注源自:http://blog.csdn.net/musen518 jmeter报告结果中会出现三个时间 1. Elapsed time 经过的时间(= Sample time = L ...
- 芒果TV真实视频地址解析
本文旨在互相学习,请勿滥用 若有幸被您引用请附加地址来源http://blog.csdn.net/feige2008/article/details/37579051 文章主要解析芒果TV的视频真实地 ...
- RISC-V首度被我国列入扶持对象,上海已成RISC-V重要“据点”
时间:2018年7月24日 16:33 摘要:近期,上海市经济信息委发布了<上海市经济信息化委关于开展2018年度第二批上海市软件和集成电路产业发展专项资金(集成电路和电子信息制造领域)项目申报 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- Looping and dictionaries
If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, pr ...