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 ...
随机推荐
- C#如何使SQLite程序集既能适应32位系统也能适应64位系统
分享5: 需求:都知道Sqlite3是分32位和64位版本的,那如果将一个Sqlite3.dll文件全适用 分析:Sqlite是种轻量级的数据库文件,使用了混合编程而成的,一部分采用非托管的C++代码 ...
- MYSQL Statement violates GTID consistency: Updates to non-transactional tables can only be done in either autocommitted statements or single-statement transactions, and never in the same statement as
[2019-04-21 10:17:20] [ERROR] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper:144] Statement viola ...
- php框架之phalcon
1.开发助手 1) 下载 git clone https://github.com/phalcon/cphalcon.git git clone https://github.com/phalcon/ ...
- git常用命令值stash
git stash(git储藏)可用于以下情形: 发现有一个类是多余的,想删掉它又担心以后需要查看它的代码,想保存它但又不想增加一个脏的提交.这时就可以考虑git stash. 使用git的时候,我们 ...
- 运行错误:Application Error - The connection to the server was unsuccessful
在模拟器上上启动ionic4.6版本 打包成的android APK,启动了很久结果弹出这个问题: Application Error - The connection to the server w ...
- 代理与hook
参考:Java 动态代理 代理是什么 为什么需要代理呢?其实这个代理与日常生活中的“代理”,“中介”差不多:比如你想海淘买东西,总不可能亲自飞到国外去购物吧,这时候我们使用第三方海淘服务比如惠惠购物助 ...
- tornado之用户验证装饰器
authenticated装饰器 为了使用Tornado的认证功能,我们需要对登录用户标记具体的处理函数.我们可以使用@tornado.web.authenticated装饰器完成它.当我们使用这个装 ...
- MySQL——修改用户密码 | 移除权限
修改用户密码 '; 移除权限 REVOKE Delete, Drop ON *.* FROM `root`@`localhost`; 权限列表
- awk 计算某一列的和
awk 计算某一列的和 我需要通过nova list 显示所有虚拟机的cpu总和,即用awk计算某一列的综合 [root@control01 ~]# nla | awk -F '|' 'BEGIN{s ...
- [模板]fhqTreap
用途 平衡树(可实现区间翻转) 原理 和treap一样,中序遍历表示权值的顺序,并且每个点有一个随机的附加值,形成一个堆来保证复杂度 但是不旋转,所有操作通过split和merge实现 分为两种spl ...