题目地址: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. 爬虫基础(五)-----scrapy框架简介

    ---------------------------------------------------摆脱穷人思维 <五> :拓展自己的视野,适当做一些眼前''无用''的事情,防止进入只关 ...

  2. OO第二单元总结——多线程电梯

    第五次作业分析 1.设计策略 调度器采用单例模式,内部设请求队列,对请求队列的一切操作(查.增.删)都在调度器内完成,且都要求串行,从而确保线程安全.接收器和电梯是两个线程:接收器接受请求调用调度器来 ...

  3. WebStorm 2018激活码

    2RRJMBXW33-eyJsaWNlbnNlSWQiOiIyUlJKTUJYVzMzIiwibGljZW5zZWVOYW1lIjoi5b285bK4IHNvZnR3YXJlMiIsImFzc2lnb ...

  4. 简述layui前端ui框架的使用

    官方网址:https://www.layui.com/demo/upload.html

  5. windows编程 进程的创建销毁和分析

    Windows程序设计:进程 进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动,在Windows编程环境下,主要由两大元素组成: • 一个是操作系统用来管理进程的内核对象.操作系统使用内 ...

  6. 第一章 Python基本语法元素分析(二)

    1.3   实例1:温度转换 根据华氏和摄氏温度定义,利用转换公式如下: C=(F-32)/1.8 F=C*1.8+32 代码如下: 运行结果: 1.4   Python程序语法元素分析 注释:不被程 ...

  7. Python IDLE 代码高亮主题

    Python IDLE 代码高亮主题 使用方法: 打开C盘我的 C:\Documents and Settings\你的用户名.idlerc文件夹 里面会有一个 config-highlight.cf ...

  8. 【dp】合唱队形

    题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K,他们的身高分别为 ...

  9. 使用sshpass同时更新一台ubuntu和一台CentOS

    1.在ubuntu上安装sshpass sudo apt install sshpass 2.分别在两台的root路径下放上升级脚本: cent:/root/upgrade.sh #!/bin/bas ...

  10. Java 8 特性 —— 函数式接口

    函数式接口 概述:接口中只有一个抽象方法. 函数式接口,即适用于函数式编程场景的接口.而 Java 中的函数式编程体现就是 Lambda,所以函数式接口就是可以适用于 Lambda 使用的接口.只有确 ...