-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72644608.cpp
47 lines (47 loc) · 853 Bytes
/
72644608.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
int arr[1000000+100];
int main()
{
int n;
cin>>n;
char c;
int one=0;
scanf("%c",&c);
for(int i=1;i<=n;i++)
{
scanf("%c",&c);
if(c=='(') arr[i]=0;
else arr[i]=1;
if(arr[i]) one++;
}
if(n!= 2*one)
{
cout<<-1<<endl;
return 0;
}
one=0;
int zero=0;
int index=0;
int res=0;
while(index<n)
{
while(one<=zero && index<=n)
{
index++;
if(arr[index]) one++;
else zero++;
}
if(index>=n) break;
res++;
while(one>zero && index<=n)
{
index++;
if(arr[index]) one++;
else zero++;
res++;
}
}
cout<<res<<endl;
return 0;
}