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

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

就改一下。

代码如下:

#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. OD调试器调试Delphi程序按钮事件断点方法

    这几天我调试一个Delphi程序,总是无法断点按钮事件,像我这样的菜鸟断点按钮事件真心累啊.所以我分享一下我下断点的经验!希望新手们少走弯路!!   工具/原料   OD调试器 Delphi程序 方法 ...

  2. 【嵌入式Linux+ARM】GPIO操作

    1.GPIO介绍 GPIO(general purpose i/o ports)意思为通用输入/输出端口,通俗的说就是一些引脚. 我们可以通过它们输出高低电平 或 读入引脚的状态. s3c2440中有 ...

  3. [WASM] Access WebAssembly Memory Directly from JavaScript

    While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless u ...

  4. ng-options bug解决方案(示例)

    情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ...

  5. [LeetCode][Java] Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函 ...

  6. Linux 用户和文件权限管理

    Linux —— 用户权限管理 权限: 为什么需要权限管理?    1.计算机资源有限,我们需要合理的分配计算机资源.    2.Linux是一个多用户系统,对于每一个用户来说,个人隐私的保护是十分重 ...

  7. Linux fcntl函数详解

    功能描述:根据文件描述词来操作文件的特性. 文件控制函数          fcntl -- file control 头文件: #include <unistd.h> #include ...

  8. 程序设计之另一种读写函数---writev,readv

    read()和write()系统调用每次在文件和进程的地址空间之间传送一块连续的数据.但是,应用有时也需要将分散在内存多处地方的数据连续写到文件中,或者反之.在这种情况下,如果要从文件中读一片连续的数 ...

  9. GDI泄露+改EXE名

    CDC 应该是成对使用 GetDC and ReleaseDC(不用new and delete) 泄露 1.改变生产exe名称:工程->设置->连接->输出文件名:Release/ ...

  10. XML-RPC JSON-RPC RPC是实现思路

    XML-RPC - Wikipedia https://en.wikipedia.org/wiki/XML-RPC JSON-RPC - Wikipedia https://en.wikipedia. ...