1322A. Unusual Competitions · 括号
题解
题意:
括号匹配,给一段括号,可以选择一段区间[l,r][l,r][l,r],交换其内部括号的位置使得这一段区间的括号是匹配的,代价是区间长度,问要使整个区间都是匹配的最小代价是多少
官方题解:
遇到 (((,就+1,遇到 ))),就 -1,找离-1最近的0的位置,就是这一段区间最小的代价
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10;
int a[N];
int n, m, K;
string s;int main() {ios::sync_with_stdio(0);cin >> n;cin >> s;for (int i = 1; i <= n; ++i) {a[i] = a[i - 1];if (s[i - 1] == '(') a[i]++;else a[i]--;}if(a[n]) return puts("-1"),0;int i = 1, ans = 0;while (i <= n) {if (a[i] != -1) {i++;continue;}int j = i;while (j <= n && a[j]) {j++;}ans += j - i + 1;i = j;}cout <<ans << endl;return 0;
}
1322A. Unusual Competitions · 括号相关推荐
- Unusual Competitions
Unusual Competitions A bracketed sequence is called correct (regular) if by inserting "+" ...
- Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)C. Unusual Competitions
C. Unusual Competitions time limit per test1 second memory limit per test512 megabytes inputstandard ...
- A. Unusual Competitions
滴答滴答---题目链接 A. Unusual Competitions time limit per test 1 second memory limit per test 512 megabytes ...
- CodeForces - 1323C Unusual Competitions(贪心)
题目链接:点击查看 题目大意:给出一个长度为 n 的括号序列,现在允许的操作是对于一段区间 [ l , r ] 内的括号重新排列,所需要的花费为区间长度,问如果想要使得括号序列变为正确的形式,最少花费 ...
- Unusual Competitions CodeForces - 1323C(思维)
A bracketed sequence is called correct (regular) if by inserting "+" and "1" you ...
- Codeforces 1322 A. Unusual Competitions
题意: 给出一个含有 ()( )() 的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成 "(())()","()"& ...
- C. Unusual Competitions
链接:https://codeforces.ml/contest/1323/problem/C A bracketed sequence is called correct (regular) if ...
- CodeForces - 1323 C Unusual Competitions
一.内容 A bracketed sequence is called correct (regular) if by inserting "+" and "1" ...
- Codeforces 1323 div2题解ABC
A. Even Subset Sum Problem 签到题 #include <bits/stdc++.h> using namespace std; template <type ...
最新文章
- Java/Android基础-02
- 2018 Multi-University Training Contest 6-oval-and-rectangle(hdu 6362)-题解
- 核心概念——节点/边/Combo——内置节点——Diamond
- 日发帖 发帖频率 发帖时段_先发帖
- java虚拟机常用命令工具
- 前端学习(1747):前端调试值之console用处
- YOLO,VOC数据集标注格式解析
- spring mysql中文乱码问题_解决Spring下MySQL出现乱码的问题
- [转载] 七龙珠第一部——第086话 打进前八强
- 中国邻苯二甲酸二环己酯(DCHP)行业市场供需与战略研究报告
- P2296 寻找道路
- XCode应该是从11.4开始支持Sandbox
- 高质量编程之编译警告级别
- 160个crackme 008 Andrénalin.1
- Python编写工资计算器
- 高德地图中自定义色块标识任意省市县区域
- 《信息系统布线技术》实验报告之交叉线制作
- 编程c语言黑与白,C语言黑与白问题代码及解析
- 如何使用微信、支付宝提现免手续费
- Java程序性能优化 ! .