2018 Multi-University Training Contest 1 | Balanced Sequence(暴力贪心)

本文针对平衡序列问题进行了深入探讨,通过四种不同的偏序方式进行排序,并采用贪心算法求解最大匹配值,最终实现对多种情况的有效处理。

                                     Balanced Sequence

 

传送门

今天这题,完全是我的锅,读错了题意(看成了有多少个匹配的字串)然后还决然的自己去敲了这道题,到了最后50mins在和队友关于算法完备性的争论中才发现自己读错了题......

最后再次5题gg,如果能早点发现读错题,6题兴许可能还能到第一页......

自己看题和解题思路还是不够明晰,想清楚了再去写,不然只能写bug.

 

然后这题,我们对四种偏序方式都进行一边排序,一次排序后贪心的时间复杂度为O(n)

四次的时间就是O(4n).

 

#include<bits/stdc++.h>
using namespace std;
char s[100010];


struct edge
{
    int R,L;
}mi[100005];

void init()
{
    memset(mi,0,sizeof(mi));
}

bool cmp1(edge x,edge y)
{
    if(x.R!=y.R)
        return x.R>y.R;
    return x.L>y.L;
}

bool cmp2(edge x,edge y)
{
    if(x.R!=y.R)
        return x.R<y.R;
    return x.L>y.L;
}

bool cmp3(edge x,edge y)
{
    if(x.R!=y.R)
        return x.R<y.R;
    return x.L<y.L;
}

bool cmp4(edge x,edge y)
{
    if(x.R!=y.R)
        return x.R>y.R;
    return x.L<y.L;
}

int main()
{
    int T,n,m;
    while(scanf("%d",&T)!=EOF)
    {
        for(int t=1; t<=T; t++)
        {
            init();
            scanf("%d",&n);
            int ans=0,now=0;
            for(int i=1; i<=n; i++)
            {
                now=0;
                scanf("%s",s);
                int len=strlen(s);
                for(int j=0; j<len; j++)
                {
                    if(s[j]=='(')
                        now++;
                    else
                    {
                        if(now)now--,ans+=2;
                        else mi[i].L++;
                    }
                }
                mi[i].R=now;
            }
            if(n==1){
                printf("%d\n",ans);
                continue;
            }
            sort(mi+1,mi+1+n,cmp1);
            int MaxL=mi[1].L;
            int MaxR=mi[1].R;
            int Max=ans;
            int ANS=ans;
//            cout<<MaxL<<" "<<MaxR<<endl;
            for(int i=2;i<=n;i++)
            {
                if(min(mi[i].L,MaxR)>=min(mi[i].R,MaxL))
                {
                    ans+=2*min(mi[i].L,MaxR);
                    int add=min(mi[i].L,MaxR);
                    MaxR=MaxR-add+mi[i].R;
                    MaxL+=mi[i].L-add;
//                    cout<<min(mi[i].L,MaxR)<<endl;
                }
                else
                {
                    ans+=2*min(mi[i].R,MaxL);
                    int add=min(mi[i].R,MaxL);
                    MaxL=MaxL-add+mi[i].L;
                    MaxR+=mi[i].R-add;
//                    cout<<min(mi[i].R,MaxL)<<endl;
                }
            }
            Max=max(Max,ans);
            ans=ANS;
            sort(mi+1,mi+1+n,cmp2);
            MaxL=mi[1].L;
            MaxR=mi[1].R;
            for(int i=2;i<=n;i++)
            {
                if(min(mi[i].L,MaxR)>=min(mi[i].R,MaxL))
                {
                    ans+=2*min(mi[i].L,MaxR);
                    int add=min(mi[i].L,MaxR);
                    MaxR=MaxR-add+mi[i].R;
                    MaxL+=mi[i].L-add;
//                    cout<<min(mi[i].L,MaxR)<<endl;
                }
                else
                {
                    ans+=2*min(mi[i].R,MaxL);
                    int add=min(mi[i].R,MaxL);
                    MaxL=MaxL-add+mi[i].L;
                    MaxR+=mi[i].R-add;
//                    cout<<min(mi[i].R,MaxL)<<endl;
                }
            }
            Max=max(Max,ans);
            ans=ANS;
            sort(mi+1,mi+1+n,cmp3);
            MaxL=mi[1].L;
            MaxR=mi[1].R;
            for(int i=2;i<=n;i++)
            {
                if(min(mi[i].L,MaxR)>=min(mi[i].R,MaxL))
                {
                    ans+=2*min(mi[i].L,MaxR);
                    int add=min(mi[i].L,MaxR);
                    MaxR=MaxR-add+mi[i].R;
                    MaxL+=mi[i].L-add;
//                    cout<<min(mi[i].L,MaxR)<<endl;
                }
                else
                {
                    ans+=2*min(mi[i].R,MaxL);
                    int add=min(mi[i].R,MaxL);
                    MaxL=MaxL-add+mi[i].L;
                    MaxR+=mi[i].R-add;
//                    cout<<min(mi[i].R,MaxL)<<endl;
                }
            }
            Max=max(Max,ans);
            ans=ANS;
            sort(mi+1,mi+1+n,cmp4);
            MaxL=mi[1].L;
            MaxR=mi[1].R;
            for(int i=2;i<=n;i++)
            {
                if(min(mi[i].L,MaxR)>=min(mi[i].R,MaxL))
                {
                    ans+=2*min(mi[i].L,MaxR);
                    int add=min(mi[i].L,MaxR);
                    MaxR=MaxR-add+mi[i].R;
                    MaxL+=mi[i].L-add;
//                    cout<<min(mi[i].L,MaxR)<<endl;
                }
                else
                {
                    ans+=2*min(mi[i].R,MaxL);
                    int add=min(mi[i].R,MaxL);
                    MaxL=MaxL-add+mi[i].L;
                    MaxR+=mi[i].R-add;
//                    cout<<min(mi[i].R,MaxL)<<endl;
                }
            }
            Max=max(Max,ans);
            printf("%d\n",Max);
        }
    }
    return 0;
}

 

用户表: Users +-------------+---------+ | Column Name | Type | +-------------+---------+ | user_id | int | | user_name | varchar | +-------------+---------+ user_id 是该表的主键(具有唯一值的列)。 该表中的每行包括用户 ID 和用户名。 注册表: Register +-------------+---------+ | Column Name | Type | +-------------+---------+ | contest_id | int | | user_id | int | +-------------+---------+ (contest_id, user_id) 是该表的主键(具有唯一值的列的组合)。 该表中的每行包含用户的 ID 和他们注册的赛事。 编写解决方案统计出各赛事的用户注册百分率,保留两位小数。 返回的结果表按 percentage 的 降序 排序,若相同则按 contest_id 的 升序 排序。 返回结果如下示例所示。 示例 1: 输入: Users 表: +---------+-----------+ | user_id | user_name | +---------+-----------+ | 6 | Alice | | 2 | Bob | | 7 | Alex | +---------+-----------+ Register 表: +------------+---------+ | contest_id | user_id | +------------+---------+ | 215 | 6 | | 209 | 2 | | 208 | 2 | | 210 | 6 | | 208 | 6 | | 209 | 7 | | 209 | 6 | | 215 | 7 | | 208 | 7 | | 210 | 2 | | 207 | 2 | | 210 | 7 | +------------+---------+ 输出: +------------+------------+ | contest_id | percentage | +------------+------------+ | 208 | 100.0 | | 209 | 100.0 | | 210 | 100.0 | | 215 | 66.67 | | 207 | 33.33 | +------------+------------+ 解释: 所有用户都注册了 208、209 和 210 赛事,因此这些赛事的注册率为 100% ,我们按 contest_id 的降序排序加入结果表中。 Alice 和 Alex 注册了 215 赛事,注册率为 ((2/3) * 100) = 66.67% Bob 注册了 207 赛事,注册率为 ((1/3) * 100) = 33.33%
最新发布
03-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值