CF 286C(Main Sequence-贪心括号匹配)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 286C(Main Sequence-贪心括号匹配)相关的知识,希望对你有一定的参考价值。


C. Main Sequence



time limit per test



memory limit per test



input



output



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.
  • a1,a2, ...,al and b1,b2, ...,bk are correct bracket sequences, then sequence a1,a2, ...,al,b1,b2, ...,bk
  • a1,a2, ...,al — is a correct bracket sequence, then sequence
  • CF

  • also is a correct bracket sequence, wherev(v> 0)

1, 1,  - 1, 2,  - 2,  - 1 and 3,  - 3 are correct bracket sequences, and 2,  - 3

most important correct bracket sequence x1, x2, ..., xn, consisting of nintegers. As sequence x

p1, p2, ..., pn contains types of brackets, that is, pi = |xi| (1 ≤ i ≤ n). The second sequence q1, q2, ..., qt contains t integers — 这些地方必须取负数 x1, x2, ..., xn.

p1, p2, ..., pn andq1, q2, ..., qt. Help Vova restore sequence x



Input



n (1 ≤ n ≤ 106). The second line contains n integers: p1, p2, ..., pn (1 ≤ pi ≤ 109).

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



NO" (without the quotes) if Vova is mistaken and a suitable sequence x1, x2, ..., xn

YES" (without the quotes) and in the second line print n integers x1, x2, ..., xn (|xi| = pixqj < 0). If there are multiple sequences that correspond to the encrypting, you are allowed to print any of them.



Sample test(s)



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


贪心,从后向前做,尽可能取左括号。


#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;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;




以上是关于CF 286C(Main Sequence-贪心括号匹配)的主要内容,如果未能解决你的问题,请参考以下文章

CF3D Least Cost Bracket Sequence 贪心

[CF1264B] Beautiful Sequence - 构造,贪心

[CF1264B] Beautiful Sequence - 构造,贪心

CF3D Least Cost Bracket Sequence 题解——贪心

HDU - 6299 Balanced Sequence (贪心)

2018 Multi-University Training Contest 1 Balanced Sequence(贪心)