1. 程式人生 > >uva live 7637 Balanced String (貪心)

uva live 7637 Balanced String (貪心)

std sta print names case push live gif map

題目鏈接: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

uva live 7637 Balanced String (貪心)