链接:https://www.nowcoder.com/acm/contest/185/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
给定括号长度N,给出一串括号(只包含小括号),计算出最少的交换(两两交换)次数,使整个括号序列匹配。
我们认为一个括号匹配,即对任意一个')',在其左侧都有一个'('与它匹配,且他们形成一一映射关系。
输入描述:
第一行:整数N,表示括号序列长度 第二行:一个字符串,表示括号
输出描述:
一个整数,表示最少的交换次数
示例1
输入
6 (()))(
输出
1
示例2
输入
6 )))(((
输出
2
备注:
对于80%的数据:n≤3000 对于100%的数据,n≤5×106
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
using namespace std;
typedef long long ll;
const int M=1e5+5;
const int N=2e5+5;
char s[5000005];
int main(){
int n;
scanf("%d %s",&n,s);
int ans=0;
for(int i=0;i<n;i++)
if(s[i]=='(') ans++;
else
if(ans) ans--;
printf("%d\n",(ans+1)>>1);
return 0;
}