1. 程式人生 > >HDU-6299 Balanced Sequence(貪心)

HDU-6299 Balanced Sequence(貪心)

題目:n個包含 ’(‘ 和 ’)’ 的字串,將這些字串任意排序,求能過匹配消除多少括號。

貪心排個序,WA了十幾發,看題解才知道,唉好菜啊啊啊。。還有因為沒初始化???

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
struct node{
    int l,r;
}a[maxn];
char s[maxn],st[maxn];
bool cmp(const node &a,const node&b)
{
    if(a.l<=a.r&&b.l>b.r) //)少(多 > )多(少
        return true;
    if(a.l>a.r&&b.l<=b.r) //)多(少 < )少(多
        return false;
    if(a.r>=a.l&&b.r>=b.l)//)少(多   )少(多
        return a.l<b.l;   //)少的放前面
    return a.r>b.r;       //(多的優先
}
int t,n;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s);
            int len=strlen(s),top=0;
            for(int j=0;j<len;j++)
            {
                if(s[j]==')'&&top>0&&st[top]=='(')
                {
                    ans++;
                    top--;
                }
                else st[++top]=s[j];
            }
            a[i].l=a[i].r=0;
            for(int j=1;j<=top;j++)
            if(st[j]=='(')
                a[i].l++;
            else a[i].r++;
        }
        sort(a+1,a+n+1,cmp);
        int num=0;
        for(int i=2;i<=n;i++)
        {
            num+=a[i-1].r;
            if(a[i].l>num)
            {
                ans+=num;
                num=0;
            }
            else
            {
                ans+=a[i].l;
                num-=a[i].l;
            }
        }
        printf("%d\n",ans*2);
    }
    return 0;
}

相關推薦

hdu 6299 Balanced Sequence 貪心

lin main value mathjax lose ide seq max scanf Balanced Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja

2018 Multi-University Training Contest 1 hdu 6299 Balanced Sequence貪心

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=6299   題意:n個'('和 ')'和串,任意排列的拼接中最長的子序列,滿足括號匹配的最大長度 做法:把每個串做一個匹配,最後會有 1. 只包含’(’  2

HDU-6299 Balanced Sequence貪心

題目:n個包含 ’(‘ 和 ’)’ 的字串,將這些字串任意排序,求能過匹配消除多少括號。 貪心排個序,WA了十幾發,看題解才知道,唉好菜啊啊啊。。還有因為沒初始化??? #include<bits/stdc++.h> using namespace std

HDU 6299 Balanced Sequence流水線排程貪心

題意 給出 nn 個只包含左右括號的字串,將這些字串拼起來,要求最終拼出來的括號序列中,最長的完全匹配括號子序列的長度最長,問最長子序列長度。 輸入 第一行為一個整數 T

hdu 6047 Maximum Sequence貪心

you 貪心 string -i n+1 blog html next about Description Steph is extremely obsessed with “sequence problems” that are usually seen on mag

多校1——Balanced Sequence貪心

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 t

HDU 6299 Balanced Sequence <<貪心

題意 給出至多1e5個長度不超過1e5的括號序列,問將他們排序重組後最多能有多少對合法括號 思路 先將已經匹配的括號全部去掉,然後我們的序列就只會剩下三種形式——$"((((("$,$"))))((("$,$"))))"$,然後這時候就只有序列的左右括號的數量起作用了,所以我們只需通過這個條件來對他們進

HDU 6299 Balanced Sequence 括號匹配 貪心

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 t

uva live 7637 Balanced String 貪心

std sta print names case push live gif map 題目鏈接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page

HDU 1711 Number Sequence kmp

Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task

hdu 1711 Number SequenceKMP

Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <

HDU 1050 Moving Tables貪心

Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.

hdu 6299 Balanced Sequence

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 strin

hdu 5527 Too Rich貪心

解題思路 從大的面值開始考慮,放的個數保證剩下的面值能用比當前面值小的硬幣組成。這裡只有50,500的情況比較特殊,因為有20和200可能要考慮多放一個的情況。 程式碼 #includ

隨手練——HDU-2037 時間安排貪心

int problem acm esp cout href pan push_back space HDU-2037 :http://acm.hdu.edu.cn/showproblem.php?pid=2037 最基礎的貪心題目,選取結束時間早的策略。 #inclu

HDU 1560 DNA sequenceDNA序列

memory nes rgb each 12px align div printf c++ p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibr

HDU 5063 Operation the Sequence暴力

tracking pos sun 鏈接 char height ans wrap %d HDU 5063 Operation the Sequence 題目鏈接 把操作存下來。因為僅僅有50個操作,所以每次把操作逆回去執行一遍,就能求出在原來的數列中的位置。輸出就

HDU 1051: Wooden Sticks貪心

memory others sticks mac resp setup cto ret cst Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768

HDU 4415 Assassin&#39;s Creed貪心

all ria put space clas name 它的 tor problem pid=4415">HDU 4415 題意: 壯哉我Assassin! E叔有一柄耐久度為m的袖劍,以及n個目標士兵要去解決。 每解決掉一個士兵,消耗袖劍Ai的

hdu 4882 ZCC Loves Codefires貪心

string.h struct hdu cmp using return 貪心 ace algo # include<stdio.h> # include <algorithm> # include <string.h> usin