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 ...
随机推荐
- SQL注入之DVWA平台测试mysql注入
今天主要针对mysql常用注入语句进行测试. 测试环境与工具: 测试平台:DVWA,下载地址:http://down.51cto.com/data/875088,也可下载metaspolit-tabl ...
- 区间dp 括号匹配问题
这道题目能用区间dp来解决,是因为一个大区间的括号匹配数是可以由小区间最优化选取得到(也就是满足最优子结构) 然后构造dp 既然是区间类型的dp 一般用二维 我们定义dp[i][j] 表示i~j这个区 ...
- 22-Perl Socket 编程
1.Perl Socket 编程Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...
- springMvc改造springboot2.0踩坑
1. 支持jsp applicaiton.proerties添加配置 #指定视图解析路径前缀 spring.mvc.view.prefix=/WEB-INF/jsp/ #指定视图解析后缀 spring ...
- Android 直连SQL
在工作中遇到需求需要Android直接连接SQL,看了一些人说不建议直连,但我对性能没有要求,甚至说只要在局域网内能够使用就行,简单说把手机当作一个简单的移动操作点. 代码的话,网上都有比如: htt ...
- C#选择文件保存路劲
private void button8_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrow ...
- JS基础_Unicode编码表
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python爬虫redis-ip代理池搭建几十万的ip数据--可以使用
from bs4 import BeautifulSoupimport requests,os,sys,time,random,redisfrom lxml import etreeconn = re ...
- spring注解定时器
上一篇文章写了一个在配置文件中设置时间的定时器,现在来写一个注解方式的定时器: 1.工程结构如下: 2.需要执行的代码块: package com.Task; import org.springfra ...
- 转:idea类名出现了不同的颜色
转:https://blog.csdn.net/yf_mood/article/details/88191818 在安装了git以后发现idea类名出现了不同的颜色,如下: 它们分别表示的含义: 绿色 ...