HDU6299 Balanced Sequence (多校第一场1002) (贪心)
程序员文章站
2022-08-11 16:00:24
Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6207 Accepted Submission(s): 16 ......
balanced sequence
time limit: 2000/1000 ms (java/others) memory limit: 32768/32768 k (java/others)
total submission(s): 6207 accepted submission(s): 1616
problem description
chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. a string of this type is said to be balanced:
+ if it is the empty string
+ if a and b are balanced, ab is balanced,
+ if a is balanced, (a) is balanced.
chiaki can reorder the strings and then concatenate them get a new string t. let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. chiaki would like to know the maximum value of f(t) for all possible t.
+ if it is the empty string
+ if a and b are balanced, ab is balanced,
+ if a is balanced, (a) is balanced.
chiaki can reorder the strings and then concatenate them get a new string t. let f(t) be the length of the longest balanced subsequence (not necessary continuous) of t. chiaki would like to know the maximum value of f(t) for all possible t.
input
there are multiple test cases. the first line of input contains an integer t, indicating the number of test cases. for each test case:
the first line contains an integer n (1≤n≤105) -- the number of strings.
each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
it is guaranteed that the sum of all |si| does not exceeds 5×106.
the first line contains an integer n (1≤n≤105) -- the number of strings.
each of the next n lines contains a string si (1≤|si|≤105) consisting of `(' and `)'.
it is guaranteed that the sum of all |si| does not exceeds 5×106.
output
for each test case, output an integer denoting the answer.
sample input
2
1
)()(()(
2
)
)(
sample output
4
2
题目意思就是给出几组括号序列,然后排序,使得匹配的括号数最大。
首先先处理每一串序列,最后的形式都是))) (((( , 然后就是贪心 排序四种情况。
1 #include <iostream> 2 #include <stdio.h> 3 #include <math.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <algorithm> 12 #include <sstream> 13 #include <stack> 14 using namespace std; 15 #define rep(i,a,n) for (int i=a;i<n;i++) 16 #define per(i,a,n) for (int i=n-1;i>=a;i--) 17 #define pb push_back 18 #define mp make_pair 19 #define all(x) (x).begin(),(x).end() 20 #define fi first 21 #define se second 22 #define sz(x) ((int)(x).size()) 23 #define fo freopen("in.txt", "r", stdin) 24 #define lowbit(x) (x&-x) 25 #define mem(a,b) memset(a, b, sizeof(a)) 26 typedef vector<int> vi; 27 typedef long long ll; 28 typedef pair<int,int> pii; 29 const ll mod=1000000007; 30 const int inf = 0x3f3f3f3f; 31 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} 32 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} 33 //head 34 35 const int maxn = 100010; 36 int _, n; 37 struct node{ 38 int l, r, ans;// 左, 右, 单匹配 39 bool operator < (node &x) const {//排序很重要 40 if(r >= l && x.r < x.l) return false; //右括号少的排前面 41 if(r < l && x.r >= x.l) return true; 42 if(r >= l && x.r >= x.l) return l > x.l ; //都是右括号大于左括号 左括号多的排前面 43 return r < x.r; // 都是左括号大于右括号 右括号少的排前面 44 } 45 }a[maxn]; 46 47 char s[maxn]; 48 void solve() { 49 scanf("%d", &n); 50 rep(i, 0, n) { 51 a[i].l = a[i].r = a[i].ans = 0; 52 scanf("%s", s); 53 int len = strlen(s); 54 rep(j, 0, len) { 55 if(s[j] == '(') 56 a[i].l++; 57 else { 58 if(a[i].l > 0) 59 a[i].l--, a[i].ans++; 60 else 61 a[i].r++; 62 } 63 } 64 } 65 sort(a, a+n); 66 int now = 0;//维护 ( 67 int sum = 0; 68 rep(i, 0, n) { 69 if(a[i].r > now)//如果 ‘)’ 大于 ‘(’ 70 a[i].r = now; //最多匹配 的是 ‘(’ 若不大于的话,就不用变 71 sum += a[i].r + a[i].ans;//然后累加 72 now -= a[i].r;//更新 当前的 ‘(’ 数量 73 now += a[i].l; 74 } 75 printf("%d\n", 2 * sum); 76 } 77 78 int main() { 79 for(scanf("%d", &_);_;_--) { 80 solve(); 81 } 82 }
这个排序看了好久。我理解的是 a[i] 与 x比较。 由于前两种能分出来谁前谁后,所以返回false true. 比如第一个是false 就是a[i] >= x 也就是x排在前面。 后两种不能直接看出来,就return 大小关系。
fasle 就是 与重载运算符相反的, true就是相同的。——————————————————————————————个人看法。望纠正
上一篇: VR与数字资本主义:谁的虚拟?谁的现实?
下一篇: 云计算将会有很多创业机会