Beautiful Sequence【Codeforces 1265 D】【枚举 + 欧拉通路性质】

Codeforces Round #604 (Div. 2) D


  我们发现,如果我们能让这一串数字最后都变成偶数度,那么就代表了形成了欧拉通路,并且还是欧拉回路,如果呢,存在两个奇数度的结点,那么也可能是形成了欧拉通路。再者,可以看到一个,连续的奇数度,可以看成一个奇数度,因为他们只要从一个点开始跑,就能把这些奇数度的点都跑完了,如果跑了另一个方向,那也不要紧,相当于是把它看成了两个奇数度的点而已了。

  所以,我们大可枚举起点,反正就只有4个起点,当然,也有可能存在小于4个结点的情况“1 0 0 1”显然是不行的。

  然后,我们从起点开始跑,向两边跑,先跑某个方向,跑完之后再去接着往另一个方向,再往该方向贪心。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define Min3(a, b, c) min(a, min(b, c))
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7;
int a[4], N, ans[maxN], cnt = 0, b[4];
inline bool check(int st)
{
    cnt = 0;
    ans[++cnt] = st;
    a[st]--;
    for(int i=2, now; i<=N; i++)
    {
        now = ans[i - 1];
        if(now > 0 && a[now - 1])
        {
            ans[++cnt] = now - 1;
            a[now - 1]--;
        }
        else if(now < 3 && a[now + 1])
        {
            ans[++cnt] = now + 1;
            a[now + 1]--;
        }
        else return false;
    }
    return true;
}
int main()
{
    scanf("%d%d%d%d", &a[0], &a[1], &a[2], &a[3]);
    for(int i=0; i<4; i++) b[i] = a[i];
    N = a[0] + a[1] + a[2] + a[3];
    for(int i=0; i<4; i++)
    {
        if(!a[i]) continue;
        for(int j=0; j<4; j++) a[j] = b[j];
        if(check(i))
        {
            printf("YES\n");
            for(int i=1; i<=N; i++) printf("%d%c", ans[i], i == N ? '\n' : ' ');
            return 0;
        }
    }
    printf("NO\n");
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值