首页 > 其他分享 >Codeforces 1322 A. Unusual Competitions

Codeforces 1322 A. Unusual Competitions

时间:2023-02-03 12:39:11浏览次数:55  
标签:1322 Unusual int sum pos else Competitions && ans


Codeforces 1322 A. Unusual Competitions_字符串

题意:

给出一个含有 Codeforces 1322 A. Unusual Competitions_字符串_02 的字符串,让你可以选择一个区间进行重新排序,问一共选择的区间长度是多少可以使得字符串最后变成 Codeforces 1322 A. Unusual Competitions_字符串_03

我们只需要从头开始遍历然后找到 Codeforces 1322 A. Unusual Competitions_字符串_04 这种字符,并且使得 Codeforces 1322 A. Unusual Competitions_字符串_05Codeforces 1322 A. Unusual Competitions_字符串_04

AC代码:

int n, m, p;
int ans, res, pos;
const int N = 1e6 + 5;
char s[N];
int main()
{
sd(n);
ss(s + 1);
int sum = 0;
pos = -1;
ans = 0;
rep(i, 1, n)
{
if (s[i] == '(')
sum++;
else
sum--;
if (sum < 0 && pos == -1)
pos = i;
if (pos != -1 && !sum)
ans += i - pos + 1, pos = -1;
}
if (sum != 0)
puts("-1");
else
pd(ans);
return 0;
}


标签:1322,Unusual,int,sum,pos,else,Competitions,&&,ans
From: https://blog.51cto.com/u_15952369/6035752

相关文章