链接:

https://codeforces.com/contest/1209/problem/C

题意:

You are given a sequence of n digits d1d2…dn. You need to paint all the digits in two colors so that:

each digit is painted either in the color 1 or in the color 2;

if you write in a row from left to right all the digits painted in the color 1, and then after them all the digits painted in the color 2, then the resulting sequence of n digits will be non-decreasing (that is, each next digit will be greater than or equal to the previous digit).

For example, for the sequence d=914 the only valid coloring is 211 (paint in the color 1 two last digits, paint in the color 2 the first digit). But 122 is not a valid coloring (9 concatenated with 14 is not a non-decreasing sequence).

It is allowed that either of the two colors is not used at all. Digits painted in the same color are not required to have consecutive positions.

Find any of the valid ways to paint the given sequence of digits or determine that it is impossible to do.

思路:

枚举x, 比x小的为1,比x大的为2, 同时对x分类, 考虑将右边第一位比x小的右边的x放到1,其余的放到2.

检查一下.

代码:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10; char s[MAXN];
int Col[MAXN];
int n; bool Check()
{
int mmin = 0;
for (int i = 1;i <= n;i++)
{
if (Col[i] == 2)
continue;
if (s[i] < mmin)
return false;
mmin = s[i];
}
mmin = 0;
for (int i = 1;i <= n;i++)
{
if (Col[i] == 1)
continue;
if (s[i] < mmin)
return false;
mmin = s[i];
}
return true;
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
set<int> St;
scanf("%d", &n);
scanf("%s", s+1);
bool ok = false;
for (int i = 0;i <= 9;i++)
{
for (int j = 1;j <= n;j++)
{
if (s[j]-'0' < i)
Col[j] = 1;
else if (s[j]-'0' > i)
Col[j] = 2;
}
bool flag = true;
for (int j = n;j >= 1;j--)
{
if (s[j]-'0' < i)
flag = false;
if (s[j]-'0' != i)
continue;
if (!flag)
Col[j] = 2;
else
Col[j] = 1;
}
if (Check())
{
ok = true;
break;
}
}
if (!ok)
puts("-");
else
{
for (int i = 1;i <= n;i++)
printf("%d", Col[i]);
printf("\n");
}
} return 0;
}

Codeforces Round #584 C. Paint the Digits的更多相关文章

  1. Codeforces Round #584 A. Paint the Numbers

    链接: https://codeforces.com/contest/1209/problem/A 题意: You are given a sequence of integers a1,a2,-,a ...

  2. Codeforces Round #584

    传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...

  3. Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)

    怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...

  4. Codeforces Round #584 E2. Rotate Columns (hard version)

    链接: https://codeforces.com/contest/1209/problem/E2 题意: This is a harder version of the problem. The ...

  5. Codeforces Round #584 D. Cow and Snacks

    链接: https://codeforces.com/contest/1209/problem/D 题意: The legendary Farmer John is throwing a huge p ...

  6. Codeforces Round #584 B. Koala and Lights

    链接: https://codeforces.com/contest/1209/problem/B 题意: It is a holiday season, and Koala is decoratin ...

  7. 【Educational Codeforces Round 36 C】 Permute Digits

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] //从大到小枚举第i(1..len1)位 //剩余的数字从小到大排序. //看看组成的数字是不是小于等于b //如果是的话. //说 ...

  8. Codeforces Round 584 题解

    -- A,B 先秒切,C 是个毒瘤细节题,浪费了很多时间后终于过了. D 本来是个 sb 题,然而还是想了很久,不过幸好没什么大事. E1,看了一会就会了,然而被细节坑死,好久才过. 感觉 E2 很可 ...

  9. Codeforces Round #584 (Div. 1 + Div. 2)

    Contest Page A sol 每次选最小的,然后把它的所有倍数都删掉. #include<bits/stdc++.h> using namespace std; int read( ...

随机推荐

  1. HTML札记

    HTML 指的是 超文本标记语言 (Hyper Text Markup Language) 文档后缀名: 当您保存 HTML 文件时,既可以使用 .htm 也可以使用 .html 扩展名.两者没有区别 ...

  2. PPT 中用 LaTeX 插入公式、PowerPoint 中用 LaTeX 插入公式(4)

    步骤: 1. 安装 CTex 2. 安装 IguanaTex >> 下载链接1:官网 >> 下载链接2:复制链接到迅雷或IDM下载很快 3. 将「IguanaTex_v1_56 ...

  3. C语言 - 获取系统时间 以年月日时分秒的形式输出

    ESP32需要给下位机通过UART发送时间戳,形式是年月日时分秒的十六进制数据包. #include <stdio.h> #include <time.h> int main( ...

  4. Echarts API说明文档

    theme = {        // 全图默认背景        // backgroundColor: 'rgba(0,0,0,0)',                // 默认色板        ...

  5. php去掉字符串中的最后一个字符和汉字

    ###php去掉字符串中的最后一个字符和汉字 1.php去掉字符串中的最后一个字符: //方法一: $newstr = substr($str,0,strlen($str)-1); //方法二: $n ...

  6. 关于mq的思考

    解耦场景 spark 发告警,同过kafka来解耦 削峰场景 日志采集生产环境几百台,当后续数量持续增加时,如果不加消息队列或者内存队列,可能把数据库打死 一个中间件: 为什么用 ->解决什么问 ...

  7. 关于arm 的字节对齐

    一.什么是字节对齐,为什么要对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问,这 ...

  8. (转) 从0移植uboot(五) _实现串口输出

    ref : https://www.cnblogs.com/xiaojiang1025/p/6500520.html 串口作为一种非常简单的通信方式,才是嵌入式系统调试的王道,通过设置串口输出,我们可 ...

  9. Sonya and Bitwise OR CodeForces - 1004F (线段树,分治)

    大意: 给定序列$a$, 给定整数$x$. 两种操作(1)单点修改 (2)给定区间$[l,r]$,求有多少子区间满足位或和不少于$x$. 假设不带修改. 固定右端点, 合法区间关于左端点单调的. 可以 ...

  10. (五)Redis之List

    一.List常用命令 两端添加 两端弹出 1.2. 两端添加和两端弹出 package myRedis01; import java.util.HashMap; import java.util.Li ...