题目地址: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随笔(1)动态创建表格列

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. <FineUIMvc随笔>目录 FineUIMvc随笔(1)动态创建表格列 FineUIMvc随笔(2)怎样在控件中 ...

  2. flask轻量级框架入门

    # -*- encoding: utf-8 -*- #导入Flask类, 导入重定向,url_for是简易寻址跳转, from flask import Flask,redirect,url_for, ...

  3. springboot 注册dao层 service 层

    可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...

  4. redis常见问题

    1.redis满了,怎么处理? (1)内存淘汰策略(2)集群,动态增加redis服务器(推荐) 2.val比较大时(比如50MB),会有什么影响? 因为redis是单线程,多路IO复用的,所以当一个v ...

  5. STL用法整理

    百度百科 STL是Standard Template Library的简称,中文名标准模板库,惠普实验室开发的一系列软件的统称.从根本上说,STL是一些“容器”的集合,这些“容器”有list,vect ...

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

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

  7. Quartus16.1布线优化选择,重编译可能会满足时序

    流程 (1)在默认的优化编译下,时序违例. (2)在assignments中选择setting. (3)根据需求,选择不同的优化方式,目前选择性能优先. (4)可以发现时序满足要求. 以上.

  8. 【LOJ6515】贪玩蓝月

    题目大意 有一个双端队列,每个元素是一个物品,每个物品有体积和价值两个属性. 有 \(n\) 个操作,分为 \(5\) 种:前后端插入删除,还有询问:选出一些物品,满足这些物品的体积之和模 \(p\) ...

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

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

  10. win10更改无线网卡的MAC地址

    https://blog.csdn.net/qq_31778495/article/details/80932472 前段时间电脑蹭网被禁了MAC地址,故寻找修改MAC地址的方法. 本机配置: win ...