zoukankan      html  css  js  c++  java
  • hdu6299 Balanced Sequence 贪心

    题目传送门

    题目大意:给出n个字符串,定义了平衡字符串,问这些字符串组合之后,最长的平衡字符子序列的长度。

    思路:

    首先肯定要把所有字符串先处理成全是不合法的,记录右括号的数量为a,左括号的数量为b,考虑两个字符串,这两个如果min(a1,b2)小于min(a2,b1)时,第一个字符串是不是应该排在前面呢?如果两个相同的话,当然应该吧右括号比较多的放在前面,左括号比较多的放在后面。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<string.h>
    #include<sstream>
    #include<set>
    #include<map>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<bitset>
    #define CLR(a,b) memset((a),(b),sizeof((a))) 
    using namespace std;
    typedef long long ll;
    inline int rd() {
        int f = 1; int x = 0; char s = getchar();
        while (s<'0' || s>'9') { if (s == '-')f = -1; s = getchar(); }
        while (s >= '0'&&s <= '9') { x = x * 10 + s - '0'; s = getchar(); }x *= f;
        return x;
    }
    inline ll gcd(ll a, ll b) {
        if (b == 0)return a;
        return gcd(b, a%b);
    }
    struct s
    {
        int a, b;
    }z[100010];
    char c[100010];
    bool comp(s a, s b)
    {
        int x = min(a.a, b.b);
        int y = min(a.b, b.a);
        if (x == y)
        {
            if (a.a == b.a)
            {
                return a.b < b.b;
            }
            return a.a > b.a;
        }
        return x > y;
    }
    int main(void)
    {
        int t, n, i, len, j, a, b;
        long long ans;
        scanf("%d", &t);
        while (t--)
        {
            ans = 0;
            scanf("%d", &n);
            for (i = 0; i < n; i++)
            {
                scanf("%s", c);
                len = strlen(c);
                a = 0;
                b = 0;
                for (j = 0; j < len; j++)
                {
                    if (c[j] == '(')
                    {
                        a++;
                    }
                    else
                    {
                        if (a)
                        {
                            a--;
                            ans++;
                        }
                        else
                        {
                            b++;
                        }
                    }
                }
                z[i].a = a;
                z[i].b = b;
            }
            sort(z, z + n, comp);
            a = 0;
            b = 0;
            for (i = 0; i < n; i++)
            {
                if (a >= z[i].b)
                {
                    ans += z[i].b;
                    a -= z[i].b;
                }
                else
                {
                    ans += a;
                    a = 0;
                }
                a += z[i].a;
            }
            printf("%lld
    ", ans * 2);
        }
        return 0;
    }

    Balanced Sequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 6091    Accepted Submission(s): 1577


    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
  • 相关阅读:
    转:Unity3D的四种坐标系
    在知乎回答的一个问题:C#初学者以后往WP开发走,还是往unity3d走?哪个更有前景呢
    Unity3d 引擎原理详细介绍、Unity3D引擎架构设计
    vs2010打开vs2012的sln文件
    Unity3d不支持vistual studio2012?用vs2012打开unity c#脚本进行编码的方法。
    C#学习笔记15:字符串、文件、目录的操作方法
    C#学习笔记14:面向对象继承的特点和里氏转换
    C#学习笔记13:静态方法、方法重载和ref、out参数
    C#学习笔记12:枚举、结构、数组基础学习
    C#学习笔记11:C#中的顺序结构、分支结构、循环结构
  • 原文地址:https://www.cnblogs.com/mountaink/p/9558408.html
Copyright © 2011-2022 走看看