CF1153C Serval and Parenthesis Sequence
题目地址: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的更多相关文章
- 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 ...
- Serval and Parenthesis Sequence【思维】
Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...
- cf——C. Serval and Parenthesis Sequence
括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...
- cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)
题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...
- Serval and Parenthesis Sequence CodeForces - 1153C
题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法. 合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的. 题解:既然 ...
- codeforces 1153 D
cf-551-div2-D C. Serval and Parenthesis Sequence 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的任 ...
- 【Codeforces】Codeforces Round #551 (Div. 2)
Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...
- Codeforces Round #551 (Div. 2) A-E
A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...
- Codeforces Round #551 (Div. 2) A~E题解
突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...
随机推荐
- 如何下载西门子产品CAD、3D和EPLAN文件
使用CAx下载管理器可以访问产品的最新CAD和CAE数据. 介绍 技术数据中包含一系列产品的尺寸图.可使用CAx下载管理器,如果需要更多信息. 可以在西门子全球支持数据库中快速方便地找到 3D,CAx ...
- Docker 核心技术之容器与镜像
Docker容器与镜像的关系 容器提交 – docker commit docker commit -h 作用: 根据容器生成一个新的镜像 命令格式: docker commit [OPTIONS] ...
- Java的selenium代码随笔(8)
Selenium截图方法一: Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果. F ...
- Mysql中的explain和desc
查询分析器 desc 和 explain 作用基本一样,explain速度快一点 explain 一条SQL语句出出现以下参数, 其中id,select_type,table 用于定位查询,表示本行参 ...
- $_SERVER['HTTP_REFERER']的使用
转载:http://www.5idev.com/p-php_server_http_referer.shtml 使用 $_SERVER['HTTP_REFERER'] 将很容易得到链接到当前页面的前一 ...
- 队列(FIFO)—循环队列、队列的链式存储
1 队列的定义 队列是只允许在一端(队尾)进行插入操作,而在另一端(队头)进行删除操作的线性表. 2 队列的特点 1)先进先出是队列最大的特点,是应用中非常常见的模型,例如排队: 2)队列也属于线性表 ...
- kafka依赖zookeeper原因解析及应用场景
kafka简介: kafka是一个发布订阅消息系统,由topic区分消息种类,每个topic中可以有多个partition,每个kafka集群有一个多个broker服务器组成,producer可以发布 ...
- IFE第二天
HTML是超文本标记语言,HTML5是下一代的HTML标准. HTML元素是组成HTML文档的部分,HTML属性为HTML元素提供附加信息. 文档类型<!DOCTYPE>声明帮助浏览器正确 ...
- wiki leaks file link url
wiki leaks file link url XXX发表于2010-08-07 15:54:56 原始来源:http://www.wikileaks.org/wiki/Category:China ...
- SVG矢量图学习实例
从W3school上学习了一下SVG矢量图形,感觉和HTML相比还是有一些新的元素和属性的,一时间不能全部记住,特此留下笔记,供遗忘时作为参考 <!DOCTYPE html> <!- ...