bzoj3016[usaco2012 Nov]clumsy Cows
Test instructions
Given a sequence of parentheses of length n, each modification can modify the parentheses of a position, if the parenthesis is ' (', then modified to ') ', if this parenthesis is ') ', then modify to ' (', ask at least how many changes are made to make the original bracket sequence valid. Where: () is lawful;
Ais legal, then (
A) is lawful;
A,
Bare legal, then
ABis legal. n≤100000.The following:The idea is simple, but I can't know its correctness: sweep the sequence once, if you encounter a closing parenthesis and the number of closing parentheses is greater than the number of opening parentheses, change the closing parenthesis to an opening parenthesis. After sweep statistics, if the left parenthesis more then half of the left parenthesis will be changed to a closing parenthesis.Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 using namespacestd;6 7 CharChintS,ans;8 intMain () {9Ch=GetChar ();Ten while(ch!='\ n'){ One if(ch=='(') s++;if(ch==')') {s--;if(s<0) s+=2, ans++;} Ch=GetChar (); A } -ans+=s/2; printf"%d", ans);return 0; -}
20161019
bzoj3016[usaco2012 Nov]clumsy cows*