CodeForces 508E Arthur and Brackets 贪心
?題目:
E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input standard input output standard outputNotice that the memory limit is non-standard.
Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length?2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him.
All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the?i-th opening bracket he remembers the segment?[li,?ri], containing the distance to the corresponding closing bracket.
Formally speaking, for the?i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment?[li,?ri].
Help Arthur restore his favorite correct bracket sequence!
InputThe first line contains integer?n?(1?≤?n?≤?600), the number of opening brackets in Arthur's favorite correct bracket sequence.
Next?n?lines contain numbers?li?and?ri?(1?≤?li?≤?ri?<?2n), representing the segment where lies the distance from the?i-th opening bracket and the corresponding closing one.
The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.
OutputIf it is possible to restore the correct bracket sequence by the given data, print any possible choice.
If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes).
Examples input 41 1
1 1
1 1
1 1 output ()()()() input 3
5 5
3 3
1 1 output ((())) input 3
5 5
3 3
2 2 output IMPOSSIBLE input 3
2 3
1 4
1 4 output (())()
題意:
按從左到右的順序給出n對括號的距離范圍[l,r],問這些括號是否能組成合法的括號對。
樣例二:
題解:
最右邊的一對括號距離肯定要是1,因為它右邊不會再有括號被包含了,
同理,從右往左掃,掃到第i對括號的時候,它的右邊的所有括號的長度都是確定下來的,然后從i+1到n,考慮最小的能夠被i合法包含的括號對,這里貪心找最小能夠為左邊提供更多方案,從只要有解,都不會漏掉。
時間復雜度總共O(n^2)。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdio> 6 using namespace std; 7 8 const int maxn = 666; 9 10 int n; 11 12 struct Node { 13 int x, y; 14 bool operator < (const Node& tmp) const { 15 return x>tmp.x || (x == tmp.x) && y>tmp.y; 16 } 17 }nds[maxn]; 18 19 char ans[maxn * 2]; 20 int used[maxn * 2]; 21 int fst, las; 22 23 void init() { 24 memset(used, 0, sizeof(used)); 25 } 26 27 int main() { 28 // freopen("data_in.txt","r",stdin); 29 while (scanf("%d", &n) == 1 && n) { 30 init(); 31 for (int i = 0; i<n; i++) { 32 scanf("%d%d", &nds[i].x, &nds[i].y); 33 } 34 int su = 1; 35 for (int i = n - 1; i >= 0; i--) { 36 if (nds[i].x == 1) { 37 nds[i].x = 1; 38 } 39 else { 40 int sum = 1, flag = 0; 41 for (int j = i + 1; j<n;) { 42 sum += (nds[j].x + 1); 43 if (sum >= nds[i].x&&sum <= nds[i].y) { 44 nds[i].x = sum; flag = 1; break; 45 } 46 j += (nds[j].x + 1) / 2; 47 } 48 if (flag == 0) { 49 su = 0; break; 50 } 51 } 52 } 53 if (!su) printf("IMPOSSIBLE\n"); 54 else { 55 for (int i = 0; i<n; i++) { 56 for (int j = 0; j<2 * n; j++) { 57 if (used[j] == 0) { 58 ans[j] = '('; used[j] = 1; 59 ans[j + nds[i].x] = ')'; used[j + nds[i].x] = 1; 60 break; 61 } 62 } 63 } 64 for (int i = 0; i<2 * n; i++) printf("%c", ans[i]); 65 printf("\n"); 66 } 67 } 68 return 0; 69 }?
轉載于:https://www.cnblogs.com/fenice/p/5449680.html
總結
以上是生活随笔為你收集整理的CodeForces 508E Arthur and Brackets 贪心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《机电传动控制》学习笔记10-1
- 下一篇: 【算法集中营】循环冗余校验