18年暑假多校赛第一场 1002

题目地址

http://acm.hdu.edu.cn/showproblem.php?pid=6299

分四种情况排序,消去一对括号,把’)’的 ‘(’多的放在前面。 

重载运算符里面的四种情况后面两种是前面两种的逆向。

一对括号是2,答案乘二。(说实话我没看懂题意为什么chen乘2)

include <iostream>

#include <algorithm>
#include <string>
#include <cstring>
using namespace std;

const int maxn=100005;

struct Node
{
    int l,r,sum;
    bool operator < (const struct Node &b) const
    {
        if(l>=r&&b.l<=b.r)
        return false;
        else if(l<=r&&b.l>=b.r)
        return true;
        else if(l>=r&&b.l>=b.r)
        return r>=b.r;
        else if(l<=r&&b.l<=b.r)
        return l<=b.l;
    }
}node[maxn];
char s[maxn];
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
      for(int i=0;i<n;i++)
      {
          scanf("%s",s);
          int len = strlen(s);
          node[i].l=node[i].r=node[i].sum=0; 
          for(int j=0;j<len;j++)
            {
              if(s[j]==')')//    l:')'
              {
                  if(node[i].r>0)
                  {
                      node[i].r--; 
                      node[i].sum++;
                  }
                  else
                  {
                      node[i].l++;
                  }
                  
            }
            else{
                   node[i].r++;
              }               
          }      
      }
        sort(node,node+n);
        int ans=0; int now=0;//now记录‘(’ 
        for(int i=0;i<n;i++)
        {
           ans+=node[i].sum;
           if(node[i].l>=now)
           {
               ans+=now;
               now=node[i].r;
            }
            else
            {
                ans+=node[i].l;
                now-=node[i].l;
                now+=node[i].r;
             } 
        }
        printf("%d\n",ans*2);
    }
  return 0;

猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/81179023
今日推荐