hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <string>
 6 #include <cstring>
 7 #include <algorithm>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <vector>
13 using namespace std;
14 typedef long long ll;
15 typedef unsigned long long ull;
16
17 #define Faster ios::sync_with_stdio(false),cin.tie(0)
18 #define Read freopen("in.txt", "r", stdin),freopen("out.txt", "w", stdout)
19 const int INF = 0x3f3f3f3f;
20 const int maxn = 1e5 + 5;
21 const int MOD = 1e9 + 7;
22
23 struct node{
24     int left, right;
25     bool operator < (const node& x) const {
26         if(left <= right && x.right < x.left) return false;
27         else if(left > right && x.right >= x.left) return true;
28         if(left <= right && x.right >= x.left) return left > x.left;
29         else return right < x.right;
30     }
31 }a[maxn];
32
33 int main()
34 {
35     Faster;
36     int t;
37     cin >> t;
38     while(t--){
39         int n;
40         cin >> n;
41         int ans = 0;
42         for(int i = 0;i < n;i++){
43             string s;
44             cin >> s;
45             a[i].left = a[i].right = 0;
46             for(int j = 0;j < s.size();j++){
47                 if(s[j] == ‘(‘){
48                     a[i].left++;
49                 }
50                 else if(s[j] == ‘)‘){
51                     if(a[i].left > 0){
52                         a[i].left--;
53                         ans += 2;
54                     }
55                     else{
56                         a[i].right++;
57                     }
58                 }
59             }
60         }
61         sort(a, a+n);
62         int now = 0;    //记录有多少左括号没有匹配
63         for(int i = 0;i < n;i++){
64             if(a[i].right > now)
65                 a[i].right = now;
66             ans += a[i].right*2;
67             now -= a[i].right;
68             now += a[i].left;
69         }
70         cout << ans << endl;
71     }
72     return 0;
73 }

原文地址:https://www.cnblogs.com/jaydenouyang/p/9368955.html

时间: 2024-10-10 00:42:28

hdu 6299 Balanced Sequence( 2018 Multi-University Training Contest 1 )的相关文章

hdu 6299 Balanced Sequence (贪心)

Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6311    Accepted Submission(s): 1648 Problem Description Chiaki has n strings s1,s2,-,sn consisting of '(' and ')'. A string of

HDU 6299 Balanced Sequence &lt;&lt;贪心

题意 给出至多1e5个长度不超过1e5的括号序列,问将他们排序重组后最多能有多少对合法括号 思路 先将已经匹配的括号全部去掉,然后我们的序列就只会剩下三种形式——$"((((("$,$"))))((("$,$"))))"$,然后这时候就只有序列的左右括号的数量起作用了,所以我们只需通过这个条件来对他们进行排序,具体来讲,全是左括号的放最左边,全是右括号的放最右边,中间的看他怎么拼起来多怎么放.代码实现上,如果直接按这个思路写的话会re到死...具

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2

思路来源于 FXXL - - 一个比较奇怪的地方就是第三步可以不做,也就是ans至少为1,听说场内有提问的,然后 admin 说可以不做- - (wa的我心烦) /* HDU 6049 - Sdjpx Is Happy [ 枚举,剪枝 ] | 2017 Multi-University Training Contest 2 题意: 长度为N的排列 N <= 3000 排序分三个步骤: 1.原数组分为不相交的K段 2.每段都独立排序 3.选择其中两段swap 问按步骤能成功排序的K能取到的最大是多

hdu 5802 Windows 10(2016 Multi-University Training Contest 6——贪心+dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5802 Windows 10 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 333 Problem Description Long long ago, there was a

2018 Nowcoder Multi-University Training Contest 2

Practice Link A. run 题意: 白云每次可以移动\(1\)米或者\(k\)米,询问移动的米数在\([L, R]\)范围内的方案数有多少. 思路: \(dp[i][2]\)表示到第\(i\)米,是通过\(1\)米的方式过来的还是\(k\)米的方式过来的,递推即可. 代码: #include <bits/stdc++.h> using namespace std; #define N 100010 const int p = 1e9 + 7; int f[N][2], g[N];

2018 Nowcoder Multi-University Training Contest 1

Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdots, a_n\)中有多少个不同的数. 思路: 先分别离线求出\(a_1, \cdots a_i\)以及\(a_j, \cdots, a_n\)中有多少个不同的数. 再考虑有多少个数既在\([1, i]\)中也在\([j, n]\)中,再离线做一次. 考虑一个数第一次出现的时候,那么这个数下一次出现

2018 Nowcoder Multi-University Training Contest 5

Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ \begin{eqnarray*} gpa = \frac{\sum s_ic_i}{\sum s_i} \end{eqnarray*} \] 思路: 首先删去的课程越多,gpa肯定不会变得更差. 所以我们肯定是删去\(k\)门课程. 考虑二分答案,check的时候要满足: \[ \begin{eq

hdu多校1002 Balanced Sequence

Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3280 Accepted Submission(s): 846 Problem Description Chiaki has n strings s1,s2,…,sn consisting of '(' and ')'. A string of this t