CF 286C (Main Sequence-Greedy matching)

Source: Internet
Author: User

C. main Sequencetime limit per test 2 secondsmemory limit per test 256 megabytesinput standard inputoutput standard outputAs you know, Vova has recently become a new shaman in the city of Ultima Thule. so, he has got ed 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 I S a positive integer. the shamans define a correct bracket sequence as follows: An empty sequence is a correct bracket sequence. if {a1, clerk a2, clerk ,..., marshal} and {b1, then b2, then ,...,   bk} are correct bracket sequences, then sequence {a1, Segment a2, etc ,..., marshal, sector b1, Region b2, region ,..., inclubk} (their concatenation) also is a correct bracket sequence. if {a1, clerk a2, clerk ,..., marshal}-is a correct bracket sequence, then sequence also is a correct bracket Sequence, where v (v sequence> sequence 0) is an integer. for example, sequences {1, sequence 1, sequence-sequence 1, sequence 2, sequence-Sequence 2, sequence-sequence 1} and {3, required bytes-parameters 3} are correct bracket sequences, and {2, too many-parameters 3} is not. moreover, after Vova became a shaman, he learned the most important correct bracket sequence {x1, second x2, second ,..., using xn}, consisting of nintegers. as sequence x is the most important, Vova decided to encrypt it just in case. encrypting consists of two s Equences. the first sequence {p1, primary p2, middle ,...,   pn} contains types of brackets, that is, pi types = bytes | xi | (1 ≤ I groups ≤ limit n ). the second sequence {q1, 1_q2, middle ,...,   qt} contains t integers-these places must take negative numbers {x1, 1_x2, numbers ,..., using xn }. unfortunately, Vova forgot the main sequence. but he was lucky enough to keep the encryption: sequences {p1, limit p2, numbers ,..., using pn} and {q1, using q2, using ,...,   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 shoshould tell so. inputThe first line of the input contains integer n (1 records ≤ limit n records ≤ limit 106 ). the second line contains n integers: p1, 1_p2 ,..., when pn (1 hour ≤ hour pi ≤ hour 109 ). the third line contains integer t (0 records ≤ t records ≤ limit n), followed by t distinct integers q1, 1_q2, etc ,..., 1_qt (1 ≤ 1_qi ≤ 1_n ). the numbers in each line are se Parated by spaces. outputPrint a single string "NO" (without the quotes) if Vova is mistaken and a suitable sequence {x1, distance x2, distance ,..., using xn} doesn' t exist. otherwise, in the first line print "YES" (without the quotes) and in the second line print n integers x1, comment x2, comment ,..., limit xn (| xi | Limit = Limit pi; xqj limit <limit 0 ). if there are multiple sequences that correspond to the encrypting, you are allowed to print any of them. sample t Est (s) input21 10outputYES1-1input41 1 1 11 3outputYES1 1-1-1input31 1 10outputNOinput41 2 2 12 3 4outputYES1 2-2-1 greedy, done from the back forward, use left brackets as much as possible. [Cpp] # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <functional> # include <algorithm> # include <stack> using namespace std; # define MAXN (1000000 + 10) # define MAXP (1000000000 + 10) int n, m, a [MAXN], ans [MAXN], tot; bool B [MAXN]; int s [MAXN], size = 0; int main () {memset (B, 0, sizeof (B); scanf ("% d", & n ); tot = n + 1; if (n % 2) {printf ("NO \ n"); return 0 ;}for (int I = 1; I <= n; I ++) scanf ("% D", & a [I]); scanf ("% d", & m); for (int I = 1; I <= m; I ++) {int p; scanf ("% d", & p); B [p] = 1 ;}for (int I = n; I --) {if (size = 0 | s [size]! = A [I] | B [I]) {s [++ size] = a [I]; ans [-- tot] =-a [I];} else ans [-- tot] = s [size --];} if (size) printf ("NO \ n"); else {printf ("YES \ n "); for (int I = 1; I <n; I ++) printf ("% d", ans [I]); printf ("% d \ n ", ans [n]);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.