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. isEmpty 和 isBlank 区别

    isEmpty 和 isBlank 区别 org.apache.commons.lang.StringUtils 类提供了 String 的常用操作,最为常用的判空有如下两种 isEmpty(Stri ...

  2. Spring 配置文件注入

    一.Spring配置文件注入 package com.zxguan.demo; public class Person { private String name; private int age; ...

  3. mssql 数据库“查询处理器用尽了内部资源,无法生成查询计划。”问题的处理

    在项目中动态拼接sql语句,使用union all连接结果集,每个查询语句都使用了in(几百个数值).语句如: ,,,..............................) UNION ALL ...

  4. webmagic学习之路-2:采集安居客经纪人列表

    相比较 1 稍微成熟了一点,会用的东西多了. 正则用的不好,很多东西不会,大神轻喷! package com.action; import java.util.ArrayList; import ja ...

  5. 【科创人·独家】连续创业者高春辉的这六年:高强度投入打造全球领先的IP数据库

    您能看到:高春辉的创业观,IPIP.NET半程复盘及未来规划,连续创业的体验,小众产品趣事 您看不到:往年创业的回顾,技术干货,受制于篇幅忍痛割舍的精彩观点 文末彩蛋:<与雷军的分歧并非否定电商 ...

  6. 自定义springboot-starter

    参考: https://juejin.im/entry/5b447cbbe51d45199566f752 https://www.baeldung.com/spring-boot-custom-sta ...

  7. 分布式之协调服务Zookeeper专题第一讲

    写在前面: 再谈架构演进: 1.单体架构->tomcat(war) 演进:水平拆分(服务拆分,(用户服务,订单服务,商品服务)) 涉及问题:1).服务通信(webservice) 2).三态问题 ...

  8. centos 7 源代码 mysql-5.7.2 安装

    CENTOS MYSQL 5.7 下载MySQL 5.7 https://dev.mysql.com/downloads/mysql/5.7.html#downloads cd /usr/local/ ...

  9. 02-【servlet】

    1.什么是Servlet Servlet是JavaWeb的三大组件之一[Servlet,Filter,Listener],它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Se ...

  10. nacos 1.1.x 集群部署笔记

    Nacos 是什么? https://nacos.io/zh-cn/docs/what-is-nacos.html 服务(Service)是 Nacos 世界的一等公民.Nacos 支持几乎所有主流类 ...