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 ...
随机推荐
- [kuangbin带你飞]专题二十二 区间DP-E-POJ - 1651
区间DP模板题 做区间DP的题目的时候,我们考虑DP[i][j]的含义是什么? 由题意大概是这样的,我们可以从n个数中每次选一个我们以前没选过的数字拿走,需要消耗a[i]*a[i+1]*a[i-1]的 ...
- uniApp——v-for 动态class、动态style
:class="i.themColor" <view v-for="i in htmlJSON" class="column" :c ...
- 宝塔控制面板创建ftp后链接不上的解决方法
很多的新手在安装宝塔面板并且创建完ftp管理后链接ftp居然链接不上?有许多朋友都不知道本站q302博客也是基于宝塔控制面板管理的,本站在安装网站完成后也和你们一样ftp链接不上,后面经过多次测试之后 ...
- Pairwise 找到你的另一半
都说优秀的程序员擅长面向对象编程,但却经常找不到另一半,这是为什么呢?因为你总是把自己局限成为一个程序员,没有打开自己的思维. 这是一个社群的时代啊,在这里你应该找到与你有相同价值观但又互补的另一半. ...
- Ajax设置自定义请求头的两种方法
用自定义请求头token为例 方法一 $.ajax({ type: "post", url:"http://127.0.0.1:4564/bsky-app/templat ...
- newCachedThreadPool 的使用
newCachedThreadPool的线程池特点: (1)它是一个可以无限扩大的线程池:它比较适合处理执行时间比较小的任务:corePoolSize为0,maximumPoolSize为无限大,意味 ...
- memcache+tp3.2实现消息队列
){)){ ){));} $this->unLock();$this->resetSide('A');$this->resetSide('B');return true;} /* * ...
- Linux 下安装idea,提示svn版本太低问题
在 RedHat 6.5 虚拟机上装了 Idea 2017, 将项目代码从 Windows 共享到虚拟机中,然后 Idea 提示 svn 版本太旧, 上网查资料说 Idea 2018 不支持1.7以下 ...
- 学习java接口知识
学习java接口知识 //一个类最多只能有一个直接的父类.但是有多个间接的父类. java是单继承. class ye_01{ String name; } class fu_01 extends y ...
- 【LOJ#3097】[SNOI2019]通信(费用流)
[LOJ#3097][SNOI2019]通信(费用流) 题面 LOJ 题解 暴力就直接连\(O(n^2)\)条边. 然后分治/主席树优化连边就行了. 抄zsy代码,zsy代码是真的短 #include ...