题目地址:CF1153C Serval and Parenthesis Sequence

思路:贪心

如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) && \(n % 2 = 0\) 。

如果有解,那么 \(s_1\) ~ \(s_{n-2}\) 为一个合法的括号序列。

那么已知的 \((\) 和 \()\) 个数不能超过一半

接下来贪心:如果有解,那么一定有一组解是把 \(?\) 中左边一部分填成 \((\) ,右边一部分填成 \()\) ,且保证 \((\) 和 \()\) 个数正好为一半

那么就这样填呗

填完之后扫一遍是否合法就完了

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 6;
int n, c1, c2, v[N], mn = 1e9;
string s;

int main() {
    ios::sync_with_stdio(0);
    cin >> n;
    cin >> s;
    if (s[0] == '?') s[0] = '(';
    if (s[n-1] == '?') s[n-1] = ')';
    if (s[0] != '(' || s[n-1] != ')') {
        puts(":(");
        return 0;
    }
    if (n & 1) {
        puts(":(");
        return 0;
    }
    if (n == 2) {
        cout << s << endl;
        return 0;
    }
    for (int i = 1; i < n - 1; i++) {
        if (s[i] == '(') ++c1;
        if (s[i] == ')') ++c2;
    }
    if (c1 + 1 > n / 2 || c2 + 1 > n / 2) {
        puts(":(");
        return 0;
    }
    for (int i = 1; i < n - 1; i++) {
        if (s[i] == '?') {
            if (c1 + 1 < (n >> 1)) {
                ++c1;
                s[i] = '(';
            } else {
                ++c2;
                s[i] = ')';
            }
        }
    }
    int x = 0;
    for (int i = 1; i < n - 1; i++) {
        if (s[i] == '(') x++;
        else {
            x--;
            if (x < 0) {
                puts(":(");
                return 0;
            }
        }
    }
    cout << s << endl;
    return 0;
}

CF1153C Serval and Parenthesis Sequence的更多相关文章

  1. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  2. Serval and Parenthesis Sequence【思维】

    Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...

  3. cf——C. Serval and Parenthesis Sequence

    括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...

  4. cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)

    题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...

  5. Serval and Parenthesis Sequence CodeForces - 1153C

    题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法. 合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的. 题解:既然 ...

  6. codeforces 1153 D

    cf-551-div2-D C. Serval and Parenthesis Sequence 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的任 ...

  7. 【Codeforces】Codeforces Round #551 (Div. 2)

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

  8. Codeforces Round #551 (Div. 2) A-E

    A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...

  9. Codeforces Round #551 (Div. 2) A~E题解

    突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...

随机推荐

  1. FineUIMvc的分部视图连接器 --VS-- FineUIPro的用户控件连接器

    [FineUIPro]用户控件 在FineUIPro中,我们可以将公用的部分定义为一个用户控件,然后添加到页面中. 一个简单的示例:http://pro.fineui.com/#/usercontro ...

  2. 接口Set

    Set接口简介 java.util.Set 接口和 java.util.List 接口一样,同样继承自 Collection 接口,它与 Collection 接口中的方法基本一致,并没有对 Coll ...

  3. mysql 新建用户并赋予远程访问权限

    不多说直接上代码 [root@demo /]# mysql -u root -p #登录服务器数据库 Enter password:123xxx #1.创建一个新用户 testuser 密码为 tes ...

  4. nodejs部署方式-pm2

    目前Nodejs开发中有很多痛点,其中有一个是修改完代码以后需要我们重启服务才能看到效果.这样一次次的杀进程.重启,杀进程.重启很让人头大.程序员是最痛恨重复工作的物种,之前有了解过的同学可能知道 f ...

  5. [转帖]EXPDP dumpfile和parallel的关系

    http://blog.itpub.net/28602568/viewspace-2133375/ 转帖 EXPDP 里面 parallel 与 dumpfile 里面的文件数的关系. 但是我这里有一 ...

  6. 米卡 Mica Logo 存放处

  7. sonar 匿名内部类写法不推荐

    今天sonar扫出一个问题, 是这样说的. 我觉得有点牵强吧. 有点个人情绪在的样子. 那Java设计这种方式干嘛?

  8. SSM框架开发遇到的问题

    request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+re ...

  9. 【简】题解 AWSL090429 【市场】

    因为这有个时间的限制 并且  求的时间都是前缀和 那么 我们可以根据时间将排序 因为题中没有修改可以直接用背包预处理出答案 但是因为题目ci mi<=1e9   vi<=300 所以发现不 ...

  10. redis-cli 通过管道 --pipe 快速导入数据到redis中

    最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入 一:先看看命令 cat redis.txt | redis-cli -h 12 ...