题意:给定一个括号序列,改最少的括号,使得所有的括号匹配。

析:贪心,从左到右扫一下,然后统计一下左括号和右括号的数量,然后在统计中,如果有多了的右括号,那么就改成左括号,最后如果两括号数量不相等,

就改一下。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 100;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[105]; int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
scanf("%s", s);
int ans = 0;
int l = 0, r = 0;
for(int i = 0; i < n; ++i){
if(s[i] == ')'){
if(r == l){ ++l; ++ans; }
else ++r;
}
else ++l;
}
while(l != r){
--l; ++r;
++ans;
}
printf("%d\n", ans);
}
return 0;
}

UVaLive 7454 Parentheses (水题,贪心)的更多相关文章

  1. UVaLive 7372 Excellence (水题,贪心)

    题意:给定 n 个数,要求把其中两个分成一组,然后加和,问所有的都分好,最小数是几. 析:贪心策略,最大和是小的相加,就是最优的. 代码如下: #pragma comment(linker, &quo ...

  2. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  3. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  4. CodeForces 719B Anatoly and Cockroaches (水题贪心)

    题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...

  5. Codeforces Round #303 (Div. 2) D. Queue 水题贪心

    题目: 题意:给你n个数值,要求排列这个序列使得第k个数值的前K-1个数的和>=第k个数值的个数尽可能多: #include <iostream> #include <cstd ...

  6. UVaLive 6628 Grachten (水题,数学)

    题意:给定一个平面图形并且且给了几条连,求一条. 析:简单么,三角形相似,很简单就AC. 代码如下: #pragma comment(linker, "/STACK:1024000000,1 ...

  7. UVaLive 7371 Triangle (水题,判矩形)

    题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment( ...

  8. UVaLive 6855 Banks (水题,暴力)

    题意:给定 n 个数,让你求最少经过几次操作,把所有的数变成非负数,操作只有一种,变一个负数变成相反数,但是要把左右两边的数加上这个数. 析:由于看他们AC了,时间这么短,就暴力了一下,就AC了... ...

  9. 洛谷p1208 水题贪心 思想入门

    题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...

随机推荐

  1. 一步步走向国际乱码大赛-- 恶搞C语言

    大家都一直强调规范编码.可是这个世界上有个大师们娱乐的竞赛--国际乱码大赛. 能写出来的都是对语言深入了解的master.我从没想自己也能"恶搞"C,一直都是老老实实编码.就在前几 ...

  2. BUPT复试专题—找K小数(2011)

    https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd?tpId=67&tqId=29637&rp=0&a ...

  3. [Binary Hacking] ABI and EABI

    Following are some general papers about ABI and EABI. Entrance https://en.wikipedia.org/wiki/Applica ...

  4. 线段树区间更新,区间统计+离散化 POJ 2528 Mayor&#39;s posters

    题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报.问最后从外面能看到几张不同的海报. 由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于 ...

  5. ALERT日志中常见监听相关报错之三:ORA-609 TNS-12537 and TNS-12547 or TNS-12170 TNS-12535错误的排查

    1.11G中ALERT日志中有报错ORA-609 TNS-12537 and TNS-12547 or TNS-12170  12170, 'TNS-12535等问题的解决方法: Troublesho ...

  6. Html5培训之精髓

    一.核心技术(可去各技术官网学习) 1.html5的六大核心技术:Html5,CSS3,JavaScript,WebSocket,PhoneGap,Node.js,它们覆盖了设备端,浏览器端和云端的开 ...

  7. [WF4.0 实战] 事件驱动应用

    看到题目或许非常多人都会疑问,为什么要使用事件监听呢? 眼下的认识: 1,使用事件监听能够将工作流的结点返回值返回到client 2,能够实现等待与重新启动,相当于之前的WaitActivity创建B ...

  8. Intellij IDEA debug jar包

    打成jar包 mvn clean install -Dmaven.test.skip=true jar包保存在target目录下 启动jar Terminal控制台执行下面的命令,启动jar java ...

  9. HDU 6122 今夕何夕 【数学公式】 (2017"百度之星"程序设计大赛 - 初赛(A))

    今夕何夕 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. jsp 中 jstl c:if等标签失效问题

    <c:if test="${page == 1}"> ${s.index+1} </c:if> <c:if test="${page > ...