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 ...
随机推荐
- Aveva Marine C# 二次开发入门001
1# 引用 C:\AVEVA\Marine\OH12.1.SP4\Aveva.ApplicationFramework.dll C:\AVEVA\Marine\OH12.1.SP4\Aveva.App ...
- ptf转图片
1.spire 官方的有水印,通过引用 //private readonly static PdfDocument doc = new PdfDocument(); //public static S ...
- EntityFramework学习要点记一
一.Entity的注解属性(Annotations)不管是code first还是db first,都需要用到注解属性,至于用System.ComponentModel.DataAnnotations ...
- luogu题解 P3709 【大爷的字符串题】
题目链接: https://www.luogu.org/problemnew/show/P3709 思路: 首先我是没读懂题目的,浏览了讨论区的dalao发现才知道就是求区间众数的出现次数. 然后肯定 ...
- 转:git上传本地项目到github
转自:https://blog.csdn.net/Lucky_LXG/article/details/77849212 将本地项目上传到Github(两种简单.方便的方法) 一.第一种方法:首先你需要 ...
- 初识JavaScript对象
JavaScript对象语法.类型.属性 属性描述符(getOwnPropertyDescriptor().defineProperty()) [[Get]].[[Put]].Getter.Sette ...
- js安全类型检测
背景: 都知道js内置的类型检测,大多数情况下是不太可靠的,例如: typeof . instanceof typeof 返回一个未经计算的操作数的类型, 可以发现所有对象都是返回object ...
- hadoop 中ALL Applications 中Tracking 下History查找不到MapReduce Job 日志
运行一个Map Reduce job 想查看日志: 点击History ,找不到网页 解决办法如下: 1.其中有一个进程是需要启动的: Hadoop自带了一个历史服务器,可以通过历史服务器查看已经运行 ...
- Linux中 tr 命令详解
tr - translate or delete characters 主要用于转换和删除字符 带有最常用选项的t r命令格式为:tr -c -d -s [ "string1_to_tran ...
- Windows defender怎么才能彻底关闭?
据不久前的一项测试表明,Windows系统自带的Windows defender软件在所有参与测试的杀毒安全软件中对win10的运行速度影响最大. 而Win10系统的Windows defender会 ...