C - Paint the Digits

思路:这道题就只需要利用单调栈,将整个数组扫一遍,求得的最后的栈内元素(要求全部小于非栈内元素)的颜色为1,其余为2

那么怎么实现呢?求最后的栈内元素(要求全部小于非栈内元素)的方法:用一个变量记录下最小的被交换的栈元素(这里需要将由于元素相等而交换的情况派除在外)

int first=inf;
for(int i=1;i<=n;++i)
{
while((!s.empty())&&s.top()>=a[i])
{
if(s.top()!=a[i]) //将由于元素相等而交换的情况派除在外
first=min(fi,s.top());
s.pop();
}
s.push(a[i]);
}
while(s.top()>fi&&fi&&(!s.empty())) //最后的栈内元素要求全部小于非栈内元素
s.pop();

然后就是考虑不存在的情况,将栈内元素全部标记成1之后,在标记栈外元素的时候,分别比较前后两个数的大小,若后面的比前面的小,则不存在

代码:

// Created by CAD on 2019/9/15.
#include <bits/stdc++.h> #define fi first
#define inf 0x3f3f3f3f
#define mst(name, value) memset(name,value,sizeof(name))
using namespace std; stack<int>s;
const int maxn=2e5+5;
int a[maxn];
int t[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T; cin>>T;
while(T--)
{
mst(t,0);
while(!s.empty()) s.pop();
int n;cin>>n;
string str;
cin>>str;
for(int i=1;i<=n;++i)
a[i]=str[i-1]-'0';
int first=inf;
for(int i=1;i<=n;++i)
{
while((!s.empty())&&s.top()>=a[i])
{
if(s.top()!=a[i])
first=min(fi,s.top());
s.pop();
}
s.push(a[i]);
}
while(s.top()>fi&&fi&&(!s.empty())) s.pop();
for(int i=n;i>=1;--i)
if(a[i]<s.top()) s.pop(),t[i]=1;
else if(a[i]==s.top()) t[i]=1;
bool flag=true;
int temp=0;
for(int i=1;i<=n;++i)
if(!t[i])
{
if (a[i]<temp)
{
flag=false;
break;
} else t[i]=2,temp=a[i];
}
if(flag)
{
for(int i=1;i<=n;++i) cout<<t[i];
cout<<endl;
}
else cout<<'-'<<endl;
}
return 0;
}

Paint the Digits的更多相关文章

  1. CF1209C Paint the Digits

    CF1209C Paint the Digits 题意:给定T组数据,每组数据第一行输入数字串长度,第二行输入数字串,用数字1和2对数字串进行涂色,被1涂色的数字子串和被2涂色的数字子串拼接成新的数字 ...

  2. Codeforces Round #584 C. Paint the Digits

    链接: https://codeforces.com/contest/1209/problem/C 题意: You are given a sequence of n digits d1d2-dn. ...

  3. Codeforces Round #584

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

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

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

  5. 详解Paint的setXfermode(Xfermode xfermode)

    一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...

  6. android Canvas 和 Paint用法

    自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...

  7. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  8. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

随机推荐

  1. RPC一般指远程过程调用协议

    RPC一般指远程过程调用协议 RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...

  2. java——多线程知识点大总结

    1:理解线程的概念之前,我们有必要先理解一下进程的概念 程序(Program)是为实现特定目标或解决特定问题而用计算机语言(比如Java语言)编写的命令序列的集合. 进程指一个程序的一次执行过程   ...

  3. git创建库

    WMW@WMWGO MINGW64 /f $ cd e:               # 切换到 E 盘 WMW@WMWGO MINGW64 /e $ mkdir learngit      # 创建 ...

  4. 5.Struts2-Struts标签

    通用标签 1.property(取值) property:<s:property value="username"/> property 取值为字符串:<s:pr ...

  5. MySql学习笔记【三、表相关操作】

    创建表 CREATE TABLE [IF NOT EXISTS] table_name( column_name data_type, ... ) 如: CREATE TABLE test_table ...

  6. 迭代器遍历【List、Set、Map】

    迭代器遍历[List.Set.Map] example package boom.collection; import java.util.ArrayList; import java.util.Ha ...

  7. 在values中添加colors.xml

    如何在values中添加colors.xml文件?按钮上的文字,背景色的动态变化的xml放在res/drawable里,现在我就说静态的xml文件吧. res/values/colors.xml< ...

  8. ubantu32位 linux下hexedit的下载安装

    Hexedit软件介绍: hexedit是一个开源的完全免费的命令行软件,可用于在任何GNU / Linux操作系统下以十六进制和ASCII(美国信息交换标准代码)格式查看和编辑文件. 下载: 在so ...

  9. Linux 命令配置IP

    配置静态IP:ip addr add 192.168.18.18/24 dev eth0 启动网卡:ifup eth0/ifup ifcfg-eth0 添加默认网关路由:ip route add de ...

  10. css 模块化

    什么是css模块化思想?(what) 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好 ...