zoukankan      html  css  js  c++  java
  • 18年多校-1002 Balanced Sequence

    》》点击进入原题测试《《

     思路:自己写没写出来,想不通该怎么排序好,看了杜神代码后补题A掉的。重新理解了一下优先队列中重载小于号的含义,这里记录一下这种排序方式。

    #include<cstdio>
    #include<string>
    #include<queue>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct Node{
        int l, r, add;
        bool operator <(const Node &b)const{
            if (l >= r && b.l < b.r)
                return true;
            if (l < r && b.l >= b.r)
                return false;
            if (l >= r && b.l >= b.r)
                return r<b.r;
            return l > b.l;
        }
    };
    int main()
    {
        priority_queue<Node>que;
        int t; //cin >> t;
        scanf("%d", &t);
        char temp[100010];
        while (t--){
            
            int n; //cin >> n;
            scanf("%d", &n);
            getchar();
    
            while (n--){
                //string temp; cin >> temp;
                gets(temp);
                int len = strlen(temp);
                int tleft = 0, tright = 0, sum = 0;
                for (int i = 0; i < len; i++){
                    if (temp[i] == '(')
                        tleft++;
                    else if (temp[i] == ')'){
                        if (tleft == 0)tright++;
                        else {
                            tleft--;
                            sum += 2;
                        }
                    }
                }
                Node node;
                node.l = tright; node.r = tleft; node.add = sum;
                que.push(node);
            }
            int ans = 0, now = 0;
            while (!que.empty()){
                Node node = que.top();
                que.pop();
                if (node.l > now)
                    node.l = now;
                ans += node.add+node.l*2;
                now -= node.l;
                now += node.r;
                //cout << node.l << " " << node.r << " " << node.add << endl;
            }
            //cout << sum << endl;
            printf("%d
    ", ans);
        }
    
        return 0;
    }
  • 相关阅读:
    ssm整合之配置applicationContext-service.xml
    ssm整合之配置applicationContext-dao.xml
    ssm整合之mybatis配置文件SqlMapConfig.xml
    ssm整合之导包
    java BigDecimal工具类
    java中json依赖包
    Servlet+Json代码
    xstream+dom4j比较对象
    分析堆栈跟踪元素
    myeclipse搭建activemq 简单聊天
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9360210.html
Copyright © 2011-2022 走看看