Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
2024-10-10 22:16:03
Codeforces Round #275 (Div. 1)A. Diverse Permutation
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/482/problem/A
Description
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 n the 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.
Input
The single line of the input contains two space-separated positive integers n, k (1 ≤ k < n ≤ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Sample Input
Input
3 2
Input
3 1
Input
5 2
Sample Output
Output
1 3 2
Output
1 2 3
Output
1 3 2 4 5
HINT
By |x| we denote the absolute value of number x.
题意
从1-n的数,让你选择一些数来构造,要求每个相邻的数之间的绝对值之差有k种
题解:
按照1,n,2,n-1,3,n-2……这样子构造k-1组,然后把剩下没有遍历的都输出一下就好了
然后还有一种构造是n,1,2,n-1,3,n-2 这样子构造k-1组
这两种构造方法相差1,分别是对应k为奇数和偶数的情况
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* */
//************************************************************************************** inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main()
{
int flag[maxn];
int n,k;
int ans=;
cin>>n>>k;
if(k%==)
{
for(int i=;i<k-;i++)
{
if(i%)
{
cout<<ans<<" ";
flag[ans]=;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=;
ans++;
}
}
}
else
{
for(int i=;i<k-;i++)
{
if(i%==)
{
cout<<ans+<<" ";
flag[ans+]=;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=;
ans++;
}
}
} for(int i=;i<=n;i++)
{
if(flag[i]==)
cout<<i<<" ";
}
return ; }
最新文章
- iOS 字符串删除 DOM
- Improve Your Study Habits
- bash 中的case语法
- Redis学习手册(Hashes数据类型)
- iOS: 布局可视化语法 Visual Format Syntax
- (四)Angularjs - 小实例(2)
- 一些方便的bash命令
- laravel 汇总数据
- postgres 11 单实例最大支持多少个database?
- react 表单受控和非受控
- hibernate主配置文件的配置
- hadoop程序实例
- ant design的一些坑
- Activity优化几个结束的方法
- 本地项目通过github客户端上传到github网站上
- Texas Instruments matrix-gui-2.0 hacking -- helper_functions.php
- java并发编程(5)并发程序测试
- 无源码情况下直接修改jar里内容思路
- ExtJs学习-搭建开发环境
- [转] AS3地图拼接与战争迷雾的实现