C. Main Sequence
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
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.
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
这应该是我CF的第一道C题,看题解做的。。。。。
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=1000100;
int ps[MAXN];
int qs[MAXN];
int st[MAXN];
int n,m;
int main()
{
memset(qs,0,sizeof(qs));
cin>>n;
if(n%2==1)
{
cout<<"NO"<<endl;
return 0;
}
for(int i=1;i<=n;i++)
cin>>ps;
cin>>m;
for(int i=1;i<=m;i++)
{
int t;
cin>>t;
qs[t]=1;
}
int now=0;
for(int i=n;i>=1;i--)
{
if(!qs&&now&&ps==ps[st[now]])
{
ps[st[now]]=-ps;
now--;
}
else
{
now++;
st[now]=i;//记录应该成为负值的位数
}
}
if(now==0)
{
cout<<"YES\n";
for(int i=1;i<=n;i++)
{
cout<<ps<<" ";
}
cout<<endl;
}
else
{
cout<<"NO\n";
}
return 0;
}