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 ...
随机推荐
- UML在代码中的展现
依赖:一个类使用了另外一个类,这种关系是临时的.脆弱的. 如人需要过河,需要船,这时人.过河(船) 中船被当做参数传入,船的实现变化会影响过河方法. 聚合:体现是整体与部分.has-a的关系 ...
- Java BigDecimal类型的 加减乘除运算
原文: https://blog.csdn.net/xuwei_net/article/details/81253471 加法:add 减法:subtract 乘法:multiply 除法:divid ...
- C#中的out 参数,ref参数,值参数
大家可能在编码中或多或少的使用过out的ref,但是是否注意过他两的详细用法以及区别? 本文想介绍下详细介绍下out参数,ref参数以及一般值参数. 值参数 在使用参数时,把一个值传递给函数使用的一个 ...
- 完全自主创建Wrapper Tomcat容器
Wrapper Tomcat 使用说明1. Wrapper Tomcat 简介1) Wrapper Tomcat 是使用Apache Tomcat 整合 Java Service Wrapper 的一 ...
- 技术的极限(6): 密码朋克精神(Cypherpunk Spirit)
上一篇:技术的极限(5): 识别计算与技术背后的心智 下一篇:技术的极限(7): 处理复杂与分层 目录: ** 0x01 数据和App分离 ** 0x02 蒸汽朋克/赛博朋克/密码朋克 ** 0x03 ...
- 2016湖南省赛 [Cloned]
A.2016 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 组数 ...
- pycharm .sqlite文件拖动到Database里面为空
pycharm .sqlite文件拖动到Database里面为空 查资料得到解决方法:
- JS 输入框输入数字检查
<input id='ApplyInputNum' type='text' class='mytext form-control' align='left' onblur='InputCheck ...
- 'python'不是内部或外部命令,也不是可运行程序或批处理文件
配置两个环境变量: 我的电脑——属性——高级系统设置——环境变量——用户变量——path(新建) 1.配置python\python.exe所在的路径 path新建:C:\Users\Py ...
- Python进阶6---序列化与反序列化
序列化与反序列化*** 为什么要序列化 ? 定义 pickle库 #序列化实例 import pickle lst = 'a b c'.split() with open('test.txt','wb ...