zoukankan      html  css  js  c++  java
  • 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
    ");
            return 0;
        }
        if (a == 0 && b == 0)
        {
            if (abs(d-c) > 1)
            {
                puts("NO
    ");
                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
    ");
                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
    ");
            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;
    }
    
  • 相关阅读:
    paramiko
    Oracle 正则
    格式化输出
    pl/sql
    logging-----日志模块
    linux学习笔记01
    PHP-HTML-MYSQL表格添加删除
    费了好大劲做的比较好看的表单
    HTML框架
    两天笔记
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12000260.html
Copyright © 2011-2022 走看看