zoukankan      html  css  js  c++  java
  • HDU6299 Balanced Sequence (多校第一场1002) (贪心)

    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.
     
    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 (1n105) -- 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
    ", 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就是相同的。——————————————————————————————个人看法。望纠正

  • 相关阅读:
    利用JQuery+EasyDrag jQuery Plugin实现弹出可拖动的Div,同时向Div传值,然后返回Div选中的值 Virus
    批量(导入导出)迁移AD中的用户信息和密码到新环境中,同时保持用户在MOSS中的权限【addusers,ldifde,copypwd,UserInfo,tp_SystemID】 Virus
    修改MOSS网站的URL访问地址 Virus
    MOSS安装目录Layouts中的文件不能用SharePoint Designer修改 Virus
    Office Server 共享服务报错,原因:对路径“c\windows\system32\drivers\ect\hosts”的访问被拒绝 Virus
    微软文档翻译:【最佳实践】使用sharepoint对象模型编程时候的常见问题 Virus
    WCF自定义行为 Step by Step (一)搭建环境
    土了,Silverlight调试一例
    WCF之自定义Channel
    WCF自定义行为 Step by Step (四)第二个自定义behavior
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9609278.html
Copyright © 2011-2022 走看看