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表示,现在给出个序列,其中已知一些括号是右括号,可以再把一些左括号变右括号(正数取反)要找到一种括号匹配的方式
从后往前找,维护一个栈,如果当前的括号跟栈顶的匹配就马上匹配,否则改负号扔进栈里去。
如果当前这个括号跟栈顶的匹配,而你又选择不匹配的话,那么这个括号和栈顶都要分别再和后面的某个相同的括号匹配。
如果再后面的括号也真的存在,那么完全可以当前的和栈顶匹配,后面的自己匹配,所以当前和栈顶匹配这里也是可行的。如果后面的不存在,这样无解,只能当前的和栈顶匹配。所以当前的跟栈顶的能匹配就匹配。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 int n,m; 26 int a[1000010]; 27 int zhan[1000010],top; 28 int main() 29 { 30 n=read(); 31 for (int i=1;i<=n;i++)a[i]=read(); 32 m=read(); 33 for (int i=1;i<=m;i++)a[read()]*=-1; 34 for (int i=n;i>=1;i--) 35 { 36 if (a[i]<0)zhan[++top]=i; 37 else if (top&&a[i]+a[zhan[top]]==0)top--; 38 else if (top&&a[i]==a[zhan[top]])a[i]*=-1,top--; 39 else a[i]*=-1,zhan[++top]=i; 40 } 41 if (top)puts("NO"); 42 else {puts("YES");for (int i=1;i<=n;i++)printf("%d ",a[i]);} 43 }