zoukankan      html  css  js  c++  java
  • 2018 Multi-University Training Contest 1

    贪心

    时隔一年,开始补多校的题了。。

    很巧的一道贪心,在读入的时候就把合法的括号消去,然后对剩下的括号序列排序。

    众所周知,不合法的括号序列只有"(((", ")))", ")("这三种,所以我们可以模拟一下这三种情况。

    最后可知"((("这种形状应该放前面,")))"这种形状应该后面,最后一种放中间。

    在排序的时候,如果都是第三种形状,那么看左边括号是否多于右边,多的放前面,避免浪费。

    如果都是左边括号多于右边括号,为了让左括号尽可能匹配,应该把右括号多的放后面,如果都是左边括号小于右边括号,应该把左边括号多的放前面。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    int _, n;
    struct Node{
        int l, r, p;
        bool operator < (const Node &rhs) const {
            if(l < r && rhs.l >= rhs.r) return false;
            if(l >= r && rhs.l < rhs.r) return true;
            if(l < r && rhs.l < rhs.r) return l > rhs.l;
            return r < rhs.r;
        }
    }a[100005];
    int main(){
    
        ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    
        cin >> _;
        for(; _; _ --){
            int ans = 0;
            cin >> n;
            for(int i = 1; i <= n; i ++){
                string s;
                cin >> s;
                a[i].l = a[i].r = a[i].p = 0;
                stack<char> st;
                for(int j = 0; j < s.size(); j ++){
                    if(s[j] == '(') st.push(s[j]);
                    else{
                        if(!st.empty()) st.pop(), a[i].p ++, ans ++;
                        else a[i].r ++;
                    }
                }
                if(!st.empty()) a[i].l += st.size();
            }
            sort(a + 1, a + n + 1);
            int pre = 0;
            for(int i = 1; i < n; i ++){
                a[i].l += pre;
                ans += min(a[i].l, a[i + 1].r);
                pre = max(0, a[i].l - a[i + 1].r);
            }
            cout << ans * 2 << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    ext与xfs文件系统比较与总结
    MySQL prepare 原理
    MySQL 性能监控 4 大指标
    MySQL数据库设计规范
    What To Do When MySQL Runs Out of Memory: Troubleshooting Guide
    MySQL 8.0 —— CATS事务调度算法的性能提升
    MySQL5.7中的sql_mode默认值
    MySQL8.0——Resource Group(资源组)
    MySQL 8.0 —— 数据字典
    mysqlreport工具
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10890549.html
Copyright © 2011-2022 走看看