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

Codeforces Round #275 (Div. 2) C

时间:2014-10-25 09:19:31      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:algorithm   acm   codeforces   算法   

题目传送门:http://codeforces.com/contest/483/problem/C


题意分析:题目意思没啥好说的。

去搞排列列举必须TLE,那么就想到构造。 1,n,2,n-1,3,n-2这个样子。k/2就是需要交换的元素对数,还需要考虑一下k的奇偶去判断没交换的元素是顺序输出还是逆序输出。自己尝试下几个数据就明白了。



代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;


typedef long long LL;

int p[100005];
int main()
{
    int n,k;
    while(cin>>n>>k)
    {
        for(int i=0; i<n; i++)
        {
            p[i]=i+1;
        }
        int flag=0;
        int temp=n-1;
        int m=k/2;
        int x=k;
        while(m--)
        {
            printf("%d ",p[flag]);
            printf("%d ",p[temp]);
            flag++;
            temp--;
        }
        if(x%2==0)
        {
            for(int i=temp; i>=flag; i--)
            {
                printf("%d ",p[i]);
            }
        }
        if(x%2==1)
        {
            for(int i=flag; i<=temp; i++)
            {
                printf("%d ",p[i]);
            }
        }
        printf("\n");
    }
}


Codeforces Round #275 (Div. 2) C

标签:algorithm   acm   codeforces   算法   

原文地址:http://blog.csdn.net/notdeep__acm/article/details/40445831

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