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\n", ans);
    }

    return 0;
}

 

posted @ 2018-07-24 15:24  我只有一件白T恤  阅读(169)  评论(0编辑  收藏  举报