codeforces gym 101341 A Streets of Working Lanterns - 2(贪心+线段树)

3 篇文章 0 订阅
2 篇文章 0 订阅

题解:

大致题意为,给定n个括号组合,问能不能将这n个括号组合排序后最后变成一串合法的括号组合

括号匹配题第一反应是栈,第二反应是左括号为1,右括号为-1,前缀和。
通过第二种思想我们可以将输入的 n n n个括号组合转变成为了接上该组合,前缀至少需要 l l l个左括号,后缀可加 r r r个左括号。

之后我们将这 n n n个组合以 l l l值,小到大排序,并以该序列建立区间 r r r值最大的线段树

最后一步就是我们贪心取的思路。
首先确保的一点是,接下去取的组合中的 l l l值一定要小于当前括号的 s u m sum sum值(即可用左括号数),并且,所取的值最好是 r r r值较大的组合,这一步可以用区间求最大值来解决。

判断失败的条件是1.没取完,2.取完但是最好的sum值不为0

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <map>
#include <vector>

using namespace std;
typedef long long ll;
typedef double db;

const int N=3e5+7,m=2e6+7;


char str[N];
int mark[N],wei[N];
struct node{
    int val;
    int l;
    int id;
    int ssum;
}que[N];
struct nod{
    int l,r;
    int maxn;
    int id;
}tree[N*4];
vector<int>ans;
bool cmp(node a,node b){
    return a.l>b.l;
}
void update(int key){
    if(tree[key*2].maxn>tree[key*2+1].maxn){
        tree[key].maxn=tree[key*2].maxn;
        tree[key].id=tree[key*2].id;
    }
    else {
        tree[key].maxn=tree[key*2+1].maxn;
        tree[key].id=tree[key*2+1].id;
    }
}
void build(int key,int l,int r){
    tree[key].l=l;
    tree[key].r=r;
    if(l==r){
        tree[key].maxn=que[l].val;
        tree[key].id=que[l].id;
        return;
    }
    int mi=(l+r)/2;
    build(key*2,l,mi);
    build(key*2+1,mi+1,r);
    update(key);
}
void change(int key,int l,int num,int id){
    if(tree[key].l==l&&tree[key].r==l){
        tree[key].maxn=num;
        tree[key].id=id;
        return;
    }
    else if(tree[key].r<l||tree[key].l>l) return;
    else {
        change(key*2,l,num,id);
        change(key*2+1,l,num,id);
        update(key);
    }
}
pair<int,int> ask(int key,int l,int r){
    if(tree[key].l>r||tree[key].r<l) return make_pair(-300000,0);
    else if(tree[key].l>=l&&tree[key].r<=r){
        return make_pair(tree[key].maxn,tree[key].id);
    }
    else {
        pair<int,int>res1=ask(key*2,l,r);
        pair<int,int>res2=ask(key*2+1,l,r);
        if(res1.first>res2.first) return res1;
        return res2;
    }
}
int main(){
    //freopen("a.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str);
        int len=strlen(str);
        int sum=0;
        int minn=0;
        for(int j=0;j<len;j++){
            if(str[j]=='(') sum++;
            else sum--;
            minn=min(minn,sum);
        }
        que[i].l=minn;
        que[i].val=sum-minn;
        que[i].id=i;
        que[i].ssum=sum;
    }
    sort(que+1,que+1+n,cmp);
    for(int i=1;i<=n;i++){
        wei[que[i].id]=i;
    }
    int now=1;
    if(que[1].l!=0){
        puts("NO");
        return 0;
    }
    for(int i=0;i<=200000;i++){
        while(que[now].l>=-i&&now<=n){
            now++;
        }
        mark[i]=now-1;
    }
    build(1,1,n);
    now=0;
    int cnt=0;
    for(int i=1;i<=n;i++){
        pair<int,int>res=ask(1,1,mark[now]);
        if(res.first==-300000&&res.second==0){
            break;
        }
        now+=que[wei[res.second]].ssum;
        ans.push_back(res.second);
        change(1,wei[res.second],-300000,0);
    }
    if(now==0&&ans.size()==n){
        puts("YES");
        for(int i=0;i<ans.size();i++){
            printf("%d",ans[i]);
            if(i==ans.size()-1) puts("");
            else printf(" ");
        }
    }
    else puts("NO");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值