Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

链接:

https://codeforces.com/contest/1265/problem/D

题意:

An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s1,s2,…,sn is beautiful if |si?si+1|=1 for all 1≤i≤n?1.

Trans has a numbers 0, b numbers 1, c numbers 2 and d numbers 3. He wants to construct a beautiful sequence using all of these a+b+c+d numbers.

However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?

思路:

考虑0只能和1,3只能和2,贪心的以01, 23去放,这样0必须小于等于1, 2和3同理,再往中间插21,如果剩下的2和1相差超过1就不能满足条件。
多一个可以插在开头或者结尾。还要考虑0和1为0,2和3为0的特殊情况。
好像暴力枚举也可以。。

代码:

#include<bits/stdc++.h>
using namespace std;

int a, b, c, d;

int main()
{
    cin >> a >> b >> c >> d;
    if (((d != 0 || c != 0) && a > b) || ((a != 0 || b != 0) && d > c))
    {
        puts("NO\n");
        return 0;
    }
    if (a == 0 && b == 0)
    {
        if (abs(d-c) > 1)
        {
            puts("NO\n");
            return 0;
        }
        else
        {
            string res = "";
            for (int i = 1;i <= min(c, d);i++)
                res += "23";
            if (c > d)
                res += "2";
            else if (c < d)
                res = "3"+res;
            cout << "YES" << endl;
            for (int i = 0;i < res.length();i++)
                cout << res[i] << ' ';
            cout << endl;
            return 0;
        }
    }
    if (c == 0 && d == 0)
    {
        if (abs(a-b) > 1)
        {
            puts("NO\n");
            return 0;
        }
        else
        {
            string res = "";
            for (int i = 1;i <= min(a, b);i++)
                res += "01";
            if (a > b)
                res += "0";
            else if (a < b)
                res = "1"+res;
            cout << "YES" << endl;
            for (int i = 0;i < res.length();i++)
                cout << res[i] << ' ' ;
            cout << endl;
            return 0;
        }

    }
    int l = b-a, r = c-d;
    if (abs(l-r) > 1)
    {
        puts("NO\n");
        return 0;
    }
    string res = "";
    if (l-r == 1)
        res += "1";
    for (int i = 1;i <= a;i++)
        res += "01";
    for (int i = 1;i <= min(l, r);i++)
        res += "21";
    for (int i = 1;i <= d;i++)
        res += "23";
    if (r-l == 1)
        res += "2";
    cout << "YES" << endl;
    for (int i = 0;i < res.length();i++)
        cout << res[i] << ' ';
    cout << endl;

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12000260.html

时间: 2024-08-01 13:45:51

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)的相关文章

Codeforces Round #604 (Div. 2) A. Beautiful String

链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecutive characters are equal. For example, "ababcb", "a" and "abab" are beautiful strings, while "aaaaaa", "abaa&

Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest

链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved pi problems. Since

Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/482/problem/A Description Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct posi

Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load 题意: 给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小. 题解: 显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好. 然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了. 最大距离的最小值就是离根最远的和最近的加一加就行了. 1 #include<cstdio> 2 #define F(i,a,b) for

Codeforces Round #604 (Div. 2) D、E、F题解

Beautiful Sequence \[ Time Limit: 1000 ms\quad Memory Limit: 256 MB \] 首先我们可以考虑到 \(0\) 只能 和 \(1\) 放在一起.\(3\) 只能和 \(2\) 放在一起,那么我们想办法先把 \(0\) 和 \(3\) 凑出来,最后就剩下 \(1\) 和 \(2\) 了,我们只要把他们放在一起就可以了. 所以我们可以贪心考虑三个 \(string\),分别长成 \(0101...0101\).\(2323...2323\

Codeforces Round #604 (Div. 2)(A-E)

A. Beautiful String 题意:把'?'换成'a' or 'b' or 'c'使得相邻的两个字符不相同. 暴力枚举每个'?'前后. #include <bits/stdc++.h> using namespace std; const int MAXN=1e5+10; string s; int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ cin>>s; bool ok=t

Codeforces Round #604 (Div. 2)

https://codeforces.com/contest/1265 这场的2E是1C的不带checkpoints的版本. B - Beautiful Numbers 题意:给一个数组是一个[1,n]的permutation.对每个m∈[1,n]问[1,m]是否连续存在在这个数组中. 题解: 首先,[1,1]一定存在. 然后向指定方向扩展到2,若经过2以外的数,把2标记为不存在. 3已经被扩展,或3在区间左右?是:否. 所以每次就问新的数是不是在已有区间中或者左右,是则包含,否则扩展到有为止.

codeforces水题100道 第十三题 Codeforces Round #166 (Div. 2) A. Beautiful Year (brute force)

题目链接:http://www.codeforces.com/problemset/problem/271/A题意:给你一个四位数,求比这个数大的最小的满足四个位的数字不同的四位数.C++代码: #include <iostream> #include <algorithm> using namespace std; bool chk(int x) { int a[4]; for (int i = 0; i < 4; i ++) { a[i] = x % 10; x /= 1

Codeforces Round #353 (Div. 2) A. Infinite Sequence

Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya won