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;
}
Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)的更多相关文章
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors
链接: https://codeforces.com/contest/1265/problem/E 题意: Creatnx has n mirrors, numbered from 1 to n. E ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest
链接: https://codeforces.com/contest/1265/problem/C 题意: So the Beautiful Regional Contest (BeRC) has c ...
- Codeforces Round #604 (Div. 2) B. Beautiful Numbers
链接: https://codeforces.com/contest/1265/problem/B 题意: You are given a permutation p=[p1,p2,-,pn] of ...
- Codeforces Round #604 (Div. 2) A. Beautiful String
链接: https://codeforces.com/contest/1265/problem/A 题意: A string is called beautiful if no two consecu ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...
- Codeforces Round #604 (Div. 2) A. Beautiful String(贪心)
题目链接:https://codeforces.com/contest/1265/problem/A 题意 给出一个由 a, b, c, ? 组成的字符串,将 ? 替换为 a, b, c 中的一个字母 ...
- Codeforces Round #604 (Div. 2) B. Beautiful Numbers(双指针)
题目链接:https://codeforces.com/contest/1265/problem/B 题意 给出大小为 $n$ 的一个排列,问对于每个 $i(1 \le i \le n)$,原排列中是 ...
- Codeforces Round #604 (Div. 2) C. Beautiful Regional Contest(贪心)
题目链接:https://codeforces.com/contest/1265/problem/C 题意 从大到小给出 $n$ 只队伍的过题数,要颁发 $g$ 枚金牌,$s$ 枚银牌,$b$ 枚铜牌 ...
- Codeforces Round #604 (Div. 1) - 1C - Beautiful Mirrors with queries
题意 给出排成一列的 \(n\) 个格子,你要从 \(1\) 号格子走到 \(n\) 号格子之后(相当于 \(n+1\) 号格子),一旦你走到 \(i+1\) 号格子,游戏结束. 当你在 \(i\) ...
随机推荐
- Linux下的JMeter部署及使用
之前都是在windows环境使用JMeter,是有操作界面的.但是最近需要在Linux环境下使用,现将操作步骤记录下来 在安装JMeter之前,需要在Linux下安装JDK并配置环境变量,这里跳过 1 ...
- docker (二):容器container
docker使用入门(二):容器container docker层次结构可以分为三层,从下往上是:容器(container).服务(services).堆栈(stack),其中services定义了容 ...
- canal+kafka订阅Mysql binlog将数据异构到elasticsearch(或其他存储方式)
canal本质就是"冒充"从库,通过订阅mysql bin-log来获取数据库的更改信息. mysql配置(my.cnf) mysql需要配置my.cnf开启bin-log日志并且 ...
- node-red 安装
介绍 Node-RED背景介绍• Node-Red是IBM公司开发的一个可视化的编程工具.它允许程序员通过组合各部件来编写应用程序.这些部件可以是硬件设备(如:Arduino板子).Web API(如 ...
- VS2019删除大量空白行或者缩进大量空白行
原文:VS2019删除大量空白行或者缩进大量空白行 问题描述: 在vs编辑器的代码中有时含有大量无用的空白行,我们想删除这些大量空白行或者缩进空白行. 注: 不需要将代码复制在类似word的文本编辑器 ...
- js 算法,判断一个数组中的数字出现多少次
let arr = [11, 11, 2, 2, 5, 5, 5, 5, 3]; //创建一个map,把每个数字和其个数相对应 let countObj = {}; for (i = 0; i < ...
- ES6之promise原理
我在这里介绍了promise的原理: https://juejin.im/post/5cc54877f265da03b8585902 我在这里 仅仅张贴 我自己实现的简易promise——DiProm ...
- Java 之 Servlet 基础入门
Servlet 一.什么是 Servlet 1.概念 Servlet:server applet,是指运行在服务器端的小程序 2.Servlet servlet 就是一个接口,定义了 Java 类 ...
- Java 之 常用函数式接口
JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景,它们主要在 java.util.function 包中被提供.下面是最简单的几个接口及使用示例. 一.Supplier 接口 java ...
- javascript中的prototype和__proto__的理解
在工作中有时候会看到prototype和__proto__这两个属性,对这两个属性我一直比较蒙圈,但是我通过查阅相关资料,决定做一下总结加深自己的理解,写得不对的地方还请各位大神指出. 跟__prot ...