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的更多相关文章
- CF1209C Paint the Digits
CF1209C Paint the Digits 题意:给定T组数据,每组数据第一行输入数字串长度,第二行输入数字串,用数字1和2对数字串进行涂色,被1涂色的数字子串和被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. ...
- Codeforces Round #584
传送门 A. Paint the Numbers 签到. Code #include <bits/stdc++.h> using namespace std; typedef long l ...
- Codeforces Round #584 - Dasha Code Championship - Elimination Round (rated, open for everyone, Div. 1 + Div. 2)
怎么老是垫底啊. 不高兴. 似乎 A 掉一道题总比别人慢一些. A. Paint the Numbers 贪心,从小到大枚举,如果没有被涂色,就新增一个颜色把自己和倍数都涂上. #include< ...
- 详解Paint的setXfermode(Xfermode xfermode)
一.setXfermode(Xfermode xfermode) Xfermode国外有大神称之为过渡模式,这种翻译比较贴切但恐怕不易理解,大家也可以直接称之为图像混合模式,因为所谓的“过渡”其实就是 ...
- android Canvas 和 Paint用法
自定义view里面的onDraw方法,在这里我们可以绘制各种图形,onDraw里面有两个API我们需要了解清楚他们的用法:Canvas 和 Paint. Canvas翻译成中文就是画布的意思,Canv ...
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
随机推荐
- Neo4j WARNING: Max 1024 open files allowed, minimum of 40 000 recommended. See the Neo4j manual
you can add a line in /etc/default/neo4j: NEO4J_ULIMIT_NOFILE=60000 to set the ulimit setting (60000 ...
- 基于S7协议实现与西门子PLC通信
西门子PLC是目前工控行业市场占有额比较大的一款PLC,而且随着上位机的越来越普及, 有很多人开始考虑自己开发上位机实现与西门子PLC的通信,遇到的第一个问题就是数据通信. 其实西门子PLC提供的接口 ...
- c# 多线程使用队列顺序写日志的类 (需要再优化)
using System; using System.Collections.Generic; using System.Threading; public class LogManager { // ...
- JS基础_对象字面量
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [转载]Yacc基础
原文:https://www.ibm.com/developerworks/cn/linux/sdk/lex/index.html,摘录部分内容. Yacc的定义 Yacc 代表 Yet Anothe ...
- Express bodyParser中间件使用方式
bodyParser中间件用来解析http请求体,是express默认使用的中间件之一. 1.这个模块提供以下解析器 (1) JSON body parser (2) Raw body parser ...
- ubuntu下安装vue/cli提示No command 'vue' found
通过官方指令 npm install -g @vue/cli 安装vue脚手架提示: No command 'vue' found, did you mean: Command 'vpe' from ...
- Angular 开发环境搭建
Angular 是一款开源 JavaScript 框架,由Google 维护,用来协助单一页面应用程序运行的.它的目标是增强基于浏览器的应用,使开发和测试变得更加容易.目前最新的 Angular 版本 ...
- 最新Cocoapods 安装及使用
1.移除现有Ruby默认源 gem sources --remove https://rubygems.org/ 2.使用新的源 gem sources -a https://ruby.taobao. ...
- 记录一则ORA
应用服务器:Windows Server 2008 R2 Enterprise故障现象:项目侧同事反映应用服务器上的程序连接数据库报错:ORA-12560: TNS: 协议适配器错误 1.故障重现 2 ...