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, wherev (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 themost important correct bracket sequence {x1, x2, ..., xn}, consisting ofn 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} containst 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 sequencex 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.
Input
The first line of the input contains integer n (1 ≤ n ≤ 106). The second line containsn integers: p1, p2, ..., pn(1 ≤ pi ≤ 109).
The third line contains integer t (0 ≤ t ≤ n), followed byt distinct integers q1, q2, ..., qt(1 ≤ qi ≤ n).
The numbers in each line are separated by spaces.
Output
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 printn 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.
Examples
Input
2
1 1
0
Output
YES
1 -1
Input
4
1 1 1 1
1 3
Output
YES
1 1 -1 -1
Input
3
1 1 1
0
Output
NO
Input
4
1 2 2 1
2 3 4
Output
YES
1 2 -2 -1
大意:给定n个数的绝对值,以及t个位置,这些位置上的数必须为负数。如果前面一个数为正数,后面一个数为其相反数,就说明这个数得到匹配。问:是否存在这样的匹配?如果不存在,输出NO;否则输出YES,并输出满足条件的一个匹配。
思路:跟括号匹配一样,建立一个栈,满足条件就出栈,否则就入栈。
直接附上AC代码:
#include <bits/stdc++.h> //#pragma comment(linker, "/STACK:102400000, 102400000") using namespace std; const int maxn = 1000005; int num[maxn]; bool vis[maxn]; int n, m; stack<int> s; int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif while (~scanf("%d", &n)){ for (int i=1; i<=n; ++i) scanf("%d", num+i); memset(vis, false, sizeof(bool)*(n+1)); scanf("%d", &m); int t; while (m--){ scanf("%d", &t); vis[t] = true; } if (n & 1){ // n为奇数显然不行 printf("NO\n"); continue; } while (!s.empty()) s.pop(); for (int i=n; i>=1; --i){ if (s.empty()) s.push(i); else{ t = s.top(); if (num[t] == num[i]){ if (!vis[i] && vis[t]) s.pop(); // 已经满足前正后负,出栈 else if (!vis[i] && !vis[t]){ vis[t] = !vis[t]; s.pop(); // 不满足前正后负,改为之,出栈 } else s.push(i); } else s.push(i); } } if (!s.empty()) printf("NO\n"); else{ printf("YES\n"); for (int i=1; i<=n; ++i){ if (i > 1) printf(" "); if (vis[i]) printf("-"); printf("%d", num[i]); } printf("\n"); } } return 0; }