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
Sample Output
HINT
By |x| we denote the absolute value of number x.
题意
从1-n的数,让你选择一些数来构造,要求每个相邻的数之间的绝对值之差有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=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
int main()
{
int flag[maxn];
int n,k;
int ans=0;
cin>>n>>k;
if(k%2==0)
{
for(int i=0;i<k-1;i++)
{
if(i%2)
{
cout<<ans<<" ";
flag[ans]=1;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=1;
ans++;
}
}
}
else
{
for(int i=0;i<k-1;i++)
{
if(i%2==0)
{
cout<<ans+1<<" ";
flag[ans+1]=1;
}
else
{
cout<<n-ans<<" ";
flag[n-ans]=1;
ans++;
}
}
}
for(int i=1;i<=n;i++)
{
if(flag[i]==0)
cout<<i<<" ";
}
return 0;
}