CodeForces 286C Main Sequence
来源:互联网 发布:在淘宝代运营公司工作 编辑:程序博客网 时间:2024/05/20 09:05
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 sequencex 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 integersq1, 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 integersx1, 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
21 10
Output
YES1 -1
Input
41 1 1 11 3
Output
YES1 1 -1 -1
Input
31 1 10
Output
NO
Input
41 2 2 12 3 4
Output
YES1 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 LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);#endifwhile (~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();// 不满足前正后负,改为之,出栈}elses.push(i);}elses.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;}
- CodeForces 286C Main Sequence
- CodeForces 13C Sequence
- Codeforces 13C Sequence
- codeforces 13C. Sequence
- codeforces 13C. Sequence
- CodeForces 13C Sequence
- CodeForces 13C. Sequence
- CF 286C(Main Sequence-贪心括号匹配)
- CodeForces 13C Sequence (DP)
- Codeforces 13C Sequence dp
- Codeforces #13C: Sequence 题解
- CodeForces 287 E.Main Sequence(贪心+栈)
- Codeforces 67C. Sequence of Balls DP!
- Codeforces 392C Yet Another Number Sequence
- Codeforces #24 C. Sequence of points
- CodeForces 24C Sequence of points (几何)
- CodeForces 487C Prefix Product Sequence
- Codeforces 5C. Longest Regular Bracket Sequence
- 7、struts2 案例( 模型驱动、 防止表单重复提交--拦截器 、数据回显 、值栈 、 OGNL表达式综合运用)
- QT中如何使用.ui文件
- 第二章View与动画
- CoherentNoise(相干噪声)
- native2ascii.exe详细使用方法、native2ascii转换示例
- CodeForces 286C Main Sequence
- Linux学习日志(十)
- ARB_precision_hint_fastest,ARB_precision_hint_nicest 的意义,作用
- UVa - 1592 Database(STL综合,强推!)
- Linux学习日志(九)
- 读取spring配置文件的方法(spring读取资源文件)
- LeetCode 026 Remove Duplicates from Sorted Array
- MyBatis入门时的一些细节问题
- NYOJ-6-喷水装置(一)