cf287E Main Sequence
As you know, Vova has recently become a new shaman in the city of Ultima Thule. So, he has received the shaman knowledge about the correct bracket sequences. The shamans of Ultima Thule have been using lots of different types of brackets since prehistoric times. A bracket type is a positive integer. The shamans define a correct bracket sequence as follows:
- An empty sequence is a correct bracket sequence.
- If {a1, a2, ..., al} and {b1, b2, ..., bk} are correct bracket sequences, then sequence {a1, a2, ..., al, b1, b2, ..., bk} (their concatenation) also is a correct bracket sequence.
- If {a1, a2, ..., al} — is a correct bracket sequence, then sequence
also is a correct bracket sequence, where v (v > 0) is an integer.
For example, sequences {1, 1, - 1, 2, - 2, - 1} and {3, - 3} are correct bracket sequences, and {2, - 3} is not.
Moreover, after Vova became a shaman, he learned the most important correct bracket sequence {x1, x2, ..., xn}, consisting of n integers. As sequence x is the most important, Vova decided to encrypt it just in case.
Encrypting consists of two sequences. The first sequence {p1, p2, ..., pn} contains types of brackets, that is, pi = |xi| (1 ≤ i ≤ n). The second sequence {q1, q2, ..., qt} contains t integers — some positions (possibly, not all of them), which had negative numbers in sequence {x1, x2, ..., xn}.
Unfortunately, Vova forgot the main sequence. But he was lucky enough to keep the encryption: sequences {p1, p2, ..., pn} and {q1, q2, ..., qt}. Help Vova restore sequence x by the encryption. If there are multiple sequences that correspond to the encryption, restore any of them. If there are no such sequences, you should tell so.
The first line of the input contains integer n (1 ≤ n ≤ 106). The second line contains n integers: p1, p2, ..., pn (1 ≤ pi ≤ 109).
The third line contains integer t (0 ≤ t ≤ n), followed by t distinct integers q1, q2, ..., qt (1 ≤ qi ≤ n).
The numbers in each line are separated by spaces.
Print a single string "NO" (without the quotes) if Vova is mistaken and a suitable sequence {x1, x2, ..., xn} doesn't exist.
Otherwise, in the first line print "YES" (without the quotes) and in the second line print n integers x1, x2, ..., xn (|xi| = pi; xqj < 0). If there are multiple sequences that correspond to the encrypting, you are allowed to print any of them.
2
1 1
0
YES
1 -1
4
1 1 1 1
1 3
YES
1 1 -1 -1
3
1 1 1
0
NO
4
1 2 2 1
2 3 4
YES
1 2 -2 -1
题意是有1e9种括号,每种括号的左括号用k表示,右括号用-k表示,现在给出个序列,其中已知一些括号是右括号,可以再把一些左括号变右括号(正数取反)要找到一种括号匹配的方式
从后往前找,维护一个栈,如果当前的括号跟栈顶的匹配就马上匹配,否则改负号扔进栈里去。
如果当前这个括号跟栈顶的匹配,而你又选择不匹配的话,那么这个括号和栈顶都要分别再和后面的某个相同的括号匹配。
如果再后面的括号也真的存在,那么完全可以当前的和栈顶匹配,后面的自己匹配,所以当前和栈顶匹配这里也是可行的。如果后面的不存在,这样无解,只能当前的和栈顶匹配。所以当前的跟栈顶的能匹配就匹配。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m;
int a[];
int zhan[],top;
int main()
{
n=read();
for (int i=;i<=n;i++)a[i]=read();
m=read();
for (int i=;i<=m;i++)a[read()]*=-;
for (int i=n;i>=;i--)
{
if (a[i]<)zhan[++top]=i;
else if (top&&a[i]+a[zhan[top]]==)top--;
else if (top&&a[i]==a[zhan[top]])a[i]*=-,top--;
else a[i]*=-,zhan[++top]=i;
}
if (top)puts("NO");
else {puts("YES");for (int i=;i<=n;i++)printf("%d ",a[i]);}
}
cf 287E
cf287E Main Sequence的更多相关文章
- [CF286C] Main Sequence
问题描述 定义幸运数列: 空数列是幸运数列 如果 S 是幸运数列,那么 {r, S, -r} 也是幸运数列 (r > 0) 如果 S 和 T 都是幸运数列,那么 {S, T} 也是幸运数列 给定 ...
- 「CF286C」Main Sequence
传送门 Luogu 解题思路 看到正负号相互抵消,很容易联想到括号匹配和栈. 但由于题目钦定了一些位置只能是负数,所以我们可以这样考虑: 把负数视为右括号,正数视为左括号,然后开一个栈,从右往左遍历, ...
- MyEclipse常用插件使用教程
一.Findbugs 1. 配置 1.1 打开FindBugs视图: Windows => Show View => Other… => FindBugs => Bug Inf ...
- java 代码分析工具——JDepend
最近学习Mybatis的官方文档,看到了[项目文档]一节有很多内容没有见过,做个笔记,理解一下. 百科上的介绍,我竟然都看懂了,那就不找其他地方的资料了. JDepend 一个开放源代码的可以用来评价 ...
- [设计模式] 7 适配器模式 adapter
在 Adapter 模式的结构图中可以看到,类模式的 Adapter 采用继承的方式复用 Adaptee的接口,而在对象模式的 Adapter 中我们则采用组合的方式实现 Adaptee 的复用 类模 ...
- 设计模式 --> (5)适配器模式
适配器模式 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作.比如说我的hp笔记本,美国产品,人家美国的电压是110V的,而我们中国的 ...
- 论文阅读笔记五十二:CornerNet-Lite: Efficient Keypoint Based Object Detection(CVPR2019)
论文原址:https://arxiv.org/pdf/1904.08900.pdf github:https://github.com/princeton-vl/CornerNet-Lite 摘要 基 ...
- CHtmlEditCtrl (3): More HTML Editor Options
In this version of our HTML Editor, we'll create a floating source view/edit window and we'll implem ...
- Java静态检测工具/Java代码规范和质量检查简单介绍(转)
静态检查: 静态测试包括代码检查.静态结构分析.代码质量度量等.它可以由人工进行,充分发挥人的逻辑思维优势,也可以借助软件工具自动进行.代码检查代码检查包括代码走查.桌面检查.代码审查等,主要检查代码 ...
随机推荐
- 上下文 xx
上下文,就是指在程序中的某个位置,可以访问到的所有资源的总和. 具体说来,在程序中资源可能是一个变量.一个常量.一个类的引用等等.
- OC 导入类 #import和@class 区别
objective-c中#import和@class的区别 在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forwar ...
- vue watch 监听
1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...
- GRANT - 定义访问权限
SYNOPSIS GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } [,...] | ALL [ ...
- ftpaccess - ftpd的配置档
描述 DESCRIPTION 这个ftpaccess档案是用来配置下述功能的运作 存取功能(AccessCapabilities) autogroup<群组名称><类别>[&l ...
- CPP-基础:内存泄露及其检测工具
[转]浅谈C/C++内存泄露及其检测工具 对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题.已经有许多技术被研究出来以应对这个问题,比如 Smart Pointer,Garba ...
- 安装VC++6.0实验环境
安装VC++6.0步骤:(1)下载一个压缩包进行解压(2)点击打开解压后的文件(3)找到文件里的程序进行安装(4)等待安装完成该程序后可以试着运行一下此程序,在此我们需要了解编写程序的步骤和注意事项. ...
- 使用Microsoft Hadoop(一)
To run this program, stage some data in HDFS: 1. create a text file called input.txt that has one in ...
- 设置section的距离
在ios7中使用group类型的tableview时,第一个section距离navigationbar的距离很大,不符合这边的设计图.使用 myTableView . sectionHeaderHe ...
- (28)zabbix用户宏变量详解macro
zabbix宏变量让zabbix变得更灵活,变量可以定义在主机.模板以及全局,变量名称类似:{$MACRO},宏变量都是大写的.认识了宏变量,你会感叹zabbix越发的强大. 变量可以用于如下地方: ...