codeforces C. Diverse Permutation(构造)

简介:
题意:1...n 的全排列中 p1, p2, p3....pn中,找到至少有k个
|p1-p2| , |p2-p3|, ...|pn-1 - pn| 互不相同的元素! 

思路: 保证相邻的两个数的差值的绝对值为单调递减序列..... 

如果够k个了,最后将没有访问到的元素直接添加到末尾!


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

#define N 100005 
using namespace std;

int vis[N];
int num[N];
int main(){
    int n, k;
    scanf("%d%d", &n, &k);
    num[1] = 1;
    vis[1] = 1;
    int c = k, i;
    for(i=2; i<=n; ++i){
        if(num[i-1]-c >0 && !vis[num[i-1]-c]){
            vis[num[i-1]-c]=1;
            num[i] = num[i-1]-c;
        }
        else if(num[i-1]+c <= n && !vis[num[i-1]+c]){
            vis[num[i-1]+c]=1;
            num[i] = num[i-1]+c;
        }
        --c;
        if(c < 1) break;
    }
    for(int j=1; j<=n; ++j)
        if(!vis[j])   num[++i]=j;
    printf("%d", num[1]);
    for(int j=2; j<=n; ++j)
        printf(" %d", num[j]);
    printf("\n");
    return 0;
}


目录
相关文章
|
4月前
全排列----关于next_permutation()/prev_permutation() 函数的用法
全排列----关于next_permutation()/prev_permutation() 函数的用法
44 0
|
算法
The kth great number(小根堆思想,模板题)
The kth great number(小根堆思想,模板题)
41 0
The kth great number(小根堆思想,模板题)
codeforces446—— A.DZY Loves Sequences(DP+枚举)
codeforces446—— A.DZY Loves Sequences(DP+枚举)
84 0
codeforces446—— A.DZY Loves Sequences(DP+枚举)
|
网络架构
Codeforces Round #755 D. Guess the Permutation(交互 二分)
Codeforces Round #755 D. Guess the Permutation(交互 二分)
85 0
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
AtCoder Beginner Contest 223 D - Restricted Permutation(建图 思维 构造 拓扑排序)
112 0
|
Perl
Codeforces 1312E. Array Shrinking(区间DP 栈)
Codeforces 1312E. Array Shrinking(区间DP 栈)
90 0
|
人工智能
CF1315 C.Restoring Permutation(构造 二分 贪心)
CF1315 C.Restoring Permutation(构造 二分 贪心)
64 0
|
人工智能
[Codeforces 1286B] Numbers on Tree | 技巧构造
Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 1 to n. Each of its vertices also has an integer ai written on it. For each vertex i, Evlampiy calculated ci — the number of vertices j in the subtree of vertex i, such that a j < a i
109 0
[Codeforces 1286B] Numbers on Tree | 技巧构造
|
人工智能
[Codeforces 1589D] Guess the Permutation | 交互 思维 二分
题意 多组输入:{ 每组给出一个n,有一个长度为n的数列,在开始的时候a i = i ,有三个数i , j , k 数列反转了 [i,j−1] [j,k] 要求出这三个数,可以对系统进行询问 [ l , r ] 区间内 逆序对 的个数,会返回这个值 }
119 0