日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces 508E Arthur and Brackets 贪心

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 output

Notice 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!

Input

The 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.

Output

If 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 4
1 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 贪心的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。