码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

时间:2014-10-25 11:59:09      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:codeforces   构造   

题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation

题意:一串排列1~n。求一个序列其中相邻两项差的绝对值的个数(指绝对值不同的个数)为k个。求序列、

思路:1~k+1。构造序列前段,之后直接输出剩下的数。前面的构造可以根据,两项差的绝对值为1~k构造。


AC代码:


#include <stdio.h>
#include <string.h>
int ans[200010];
bool vis[100010];
int n,mark;
int iabs(int a)
{
    if(a<0) return -a;
    return a;
}
int main()
{
    int i,cnt,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int x,y;
        memset(vis,0,sizeof vis);
        ans[0]=1;
        x=1,y=k+1;
        cnt=k;
        for(i=1; i<=k; i++,cnt--)
        {
            int temp=ans[i-1]+cnt;
            if(temp>k+1)
                temp=ans[i-1]-cnt;
            else if(vis[temp])
                temp=ans[i-1]-cnt;
            ans[i]=temp;
            vis[temp]=true;
        }
        for(i=k+1; i<n; i++)
            ans[i]=i+1;
        for(i=0; i<n-1; i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[i]);
    }
    return 0;
}


Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

标签:codeforces   构造   

原文地址:http://blog.csdn.net/u012377575/article/details/40450519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!