CF-287E(Main Sequence) greedy(贪心)
来源:互联网 发布:软件可靠性和健壮性 编辑:程序博客网 时间:2024/05/17 23:08
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.
Input
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.
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 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.
AC代码:
#include<cstdio>#include<cstring>#define maxn 1000010int ans[maxn], s[maxn], a[maxn];bool p[maxn];int main() {int n, k, ed;memset(p, false, sizeof(p));scanf("%d", &n); ed = n;for(int i=0; i<n; i++) scanf("%d", &a[i]);scanf("%d", &k);while(k--) {int pos;scanf("%d" ,&pos);p[pos] = true;}int size = 0;if(n%2) {printf("NO\n");return 0;}for(int i=n-1; i>=0; i--) {if(!size||s[size]!=a[i]||p[ed]) {s[++size] = a[i];ans[--ed] = -a[i];} else {ans[--ed] = s[size--];}}// printf("%d\n", size);if(size!=0) printf("NO\n"); else{printf("YES\n");for(int i=0; i<n; i++) {printf("%d", ans[i]);if(i!=n-1) printf(" ");}printf("\n");}}
- CF-287E(Main Sequence) greedy(贪心)
- CodeForces 287 E.Main Sequence(贪心+栈)
- CF 286C(Main Sequence-贪心括号匹配)
- CF 177(div2) E(greedy )
- CF 277 Div2 E LIS of Sequence
- cf 486E LIS of Sequence
- CF 601B(Lipshitz Sequence-贪心)
- Codeforces Beta Round #10 E. Greedy Change(贪心)
- 贪心Greedy
- cf-279E - Beautiful Decomposition-贪心
- CF 518E(Arthur and Questions-贪心)
- 贪心,优化(Sockets,cf 732e)
- CF 496E 贪心,排序,set
- CF 490E 贪心,回溯法
- Codeforces 490E Restoring Increasing Sequence(贪心)
- CodeForces 490E Restoring Increasing Sequence(贪心)
- cf 100 c(greedy)
- 【 bzoj 4527 && CF 407 E】K-D-Sequence
- Linux一些点
- C#元数据
- pandas 模块学习总结
- ext 按钮之间的间距
- C++单例模式(懒汉和饿汉)与线程安全
- CF-287E(Main Sequence) greedy(贪心)
- VMware中linux系统与时间同步的问题
- Leetcode 32. Longest Valid Parentheses
- 线性代数笔记-3-矩阵的乘法和逆
- 2016福州大学第十三届程序设计竞赛 Problem C 平行四边形数(思维)
- java图形打印问题
- Dom操作实现二级联动下拉选择省份
- ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
- Androdi Dialog 设置宽度与屏幕宽度一样