sgu279:Bipermutations(贪心构造)

题目大意:
       ij 意味着在序列中 i 的位置在j的前面。
       构造一个长度为 2n 的序列,由 1,1,2,2...,n,n 构成。
       满足如下条件:
       1. 对于任意 i 满足ii
       2. 对于任意 ij ,满足 ijij
       3. 定义 bi,j={j,j<ij,j>i ai=[ibi,j] ,给出序列 a ,构造出满足a的序列。

分析:
       很显然我们要倒着填。
       2n 位开始,每次找最小的 i 使得ai=0填在这一位,更新后面的 a ,如果找不到,那就找位置最大的没有填过i i 填在这一位,更新后面的a,否则无解。
       为什么要先考虑 ai=0 呢?设前者的结果为 c1 ,后者的结果为 c2 ,如果 c2>c1 c1 肯定在之前就被 c2 给更新过,那么两者交换顺序不影响;如果 c2<c1 ,先放 c2 肯定会使 ac1 变小成负数,不符合题意,因此先考虑显然可以。

AC code:

#include <cstdio>
#include <algorithm>
#define mp make_pair
#define pii pair<int,int>
#define x first
#define y second
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

const int MAXN = 1009;

int n;
short a[MAXN];
short ans[MAXN<<1];
short vis[MAXN];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d", a+i);

    for(int i = (n<<1); i >= 1; --i)
    {
        int c1 = -1, c2 = -1;
        for(int j = 1; j <= n; ++j)
            if(!a[j] && !vis[j])
            {
                c1 = j;
                break;
            }
        if(c1 != -1)
        {
            ans[i] = c1;
            vis[c1] = 1;
            for(int j = 1; j <= n; ++j)
                if(!vis[j] && j < c1)
                    --a[j];
        }
        else
        {
            for(int j = n*2; j > i; --j)
                if(ans[j] > 0 && vis[ans[j]] == 1)
                {
                    c2 = ans[j];
                    break;
                }
            if(c2 == -1)
            {
                puts("NO");
                return 0;
            }
            ans[i] = -c2;
            vis[c2] = 2;
            for(int j = 1; j <= n; ++j)
                if(!vis[j] && j > c2)
                    --a[j];
        }
    }

    puts("YES");
    for(int i = 1; i <= (n<<1); ++i)
        printf("%d ", ans[i]);
    puts("");

    #ifndef ONILNE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值