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和k,让你从1,2,3...n中找出任一一种序列使得这个序列有k个不同的区间。
做法:用两个数组num1,num2分别记录前n/2个数字,和后n-n/2个数字,然后num1[1],num2[1],num1[2],num2[2]这样的顺序排下去,总共排k个这样就会有(k-1)个不同的区间然后再加上最后的1就是k种不同的区间了,具体看代码。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<stack>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define inf 0x0f0f0f0f
using namespace std;
int main()
{
int num[100005];
int num1[100005];
int num2[100005];
int n,k,i;
int tt;
while(scanf("%d%d",&n,&k)!=EOF)
{
tt=1;
for(i=1;i<=n/2;i++)
num1[i]=i;
for(i=n;i>n/2;i--)
num2[tt++]=i;
if(k==1)
{
for(i=1;i<=n;i++)
{
printf("%d",i);
if(i!=n)
printf(" ");
}
printf("\n");
}
else
{
int k1,k2;
k1=1;
k2=1;
for(i=1;i<=k;i++)
{
if(i%2==1)
printf("%d ",num1[k1++]);
else
printf("%d ",num2[k2++]);
}
if(k%2==0)
{
for(i=k2;i<tt;i++)
printf("%d ",num2[i]);
for(i=n/2;i>=k1;i--)
printf("%d ",num1[i]);
}
else
{
for(i=k1;i<=n/2;i++)
printf("%d ",num1[i]);
for(i=tt-1;i>=k2;i--)
printf("%d ",num2[i]);
}
printf("\n");
}
}
}