CodeForces 286C Main Sequence
2016-05-11 01:49
501 查看
题目链接:http://codeforces.com/problemset/problem/286/C
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.
Main Sequence
time 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 received the shaman knowledge about the correct bracket sequences. The shamans of Ultima Thule have been using lots of different typesof 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![](http://codeforces.com/predownloaded/d6/f1/d6f11e790b2d2f5d2344a548fa0c427e96e586d1.png)
Input
The first line of the input contains integern (1 ≤ n ≤ 106). The second line containsn integers:p1, p2, ..., pn(1 ≤ pi ≤ 109).The third line contains integert (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
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
大意:给定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;}
相关文章推荐
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- c++ STL容器总结之:vertor与list的应用
- C++在成员函数中使用STL的find_if函数实例
- 关于STL中list容器的一些总结
- 关于STL中的map容器的一些总结
- 浅析stl序列容器(map和set)的仿函数排序
- STL list链表的用法详细解析
- stl容器set,map,vector之erase用法与返回值详细解析
- STl中的排序算法详细解析
- 关于STL中vector容器的一些总结
- 关于STL中set容器的一些总结
- 简单说说STL的内存管理
- STL与泛型编程(1)---模板
- CppUtest发现的STL容器内存泄漏问题
- STL中算法
- STL简单应用
- vector-list-deque
- 三十分钟掌握STL
- Qt中QSet的使用