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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

uva live 7637 Balanced String (贪心)

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva live 7637 Balanced String (贪心) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5659

題意:

  你有一個只包含"(" 和 ")" 的串,每一個位置有個數值,這個數值是當前的左括號-右括號的值。

  例:()() 數值就是1010。

  給你一個打亂了的數值,要你構造出字典序最小的字符串。

題解:

  因為左括號比右括號小,所以我們要盡量的選擇左括號,選擇左括號會使得數值增大,如果這個數值不能繼續增大了我們就只能選擇右括號。

  記得要初始化

?

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <cmath> 7 #include <vector> 8 #include <queue> 9 #include <map> 10 #include <stack> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 typedef unsigned long long uLL; 15 #define ms(a, b) memset(a, b, sizeof(a)) 16 #define pb push_back 17 #define mp make_pair 18 #define eps 0.0000000001 19 #define IOS ios::sync_with_stdio(0);cin.tie(0); 20 const LL INF = 0x3f3f3f3f3f3f3f3f; 21 const int inf = 0x3f3f3f3f; 22 const int mod = 1e9+7; 23 const int maxn = 100000+10; 24 int a[maxn]; 25 int num[maxn]; 26 int last[maxn]; 27 int ans[maxn]; 28 void solve() 29 { 30 ms(num, 0); 31 ms(last, 0); 32 int n;scanf("%d", &n); 33 for(int i = 0;i<n;i++) scanf("%d", &a[i]); 34 for(int i = 0;i<n;i++){ 35 if(a[i]<0){ 36 printf("invalid");return; 37 } 38 } 39 for(int i = 0;i<n;i++){ 40 num[a[i]]++; 41 } 42 last[0]=0; 43 for(int i=1;i<=n;i++){ 44 if(num[last[i-1]+1]){ 45 last[i] = last[i-1]+1; 46 ans[i] = 1; 47 num[last[i-1]+1]--; 48 } 49 else if(num[last[i-1]-1]){ 50 last[i] = last[i-1]-1; 51 ans[i] = 0; 52 num[last[i-1]-1]--; 53 } 54 else{ 55 printf("invalid");return; 56 } 57 } 58 if(ans[n]==0){ 59 for(int i = 1;i<=n;i++) 60 if(ans[i]==1) printf("("); 61 else printf(")"); 62 }else{ 63 printf("invalid");return; 64 } 65 } 66 int main() { 67 #ifdef LOCAL 68 freopen("input.txt", "r", stdin); 69 // freopen("output.txt", "w", stdout); 70 #endif 71 // IOS 72 int t;scanf("%d", &t); 73 int cnt = 1; 74 while(t--){ 75 printf("Case %d: ", cnt++); 76 solve(); 77 printf("\n"); 78 } 79 return 0; 80 } View Code

?

轉載于:https://www.cnblogs.com/denghaiquan/p/7288100.html

總結

以上是生活随笔為你收集整理的uva live 7637 Balanced String (贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

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