/* FILE: editFormula.cpp last change: 1-Jul-2013 author: Romeo Rizzi * a linear greedy solver for problem editFormula */ #define NDEBUG // NDEBUG definita nella versione che consegno #include #ifndef NDEBUG # include // uso di cin e cout non consentito in versione finale #endif #include using namespace std; int half_rounded_up(int n) { return n/2 + (n%2); } int main() { int n; // lunghezza della stringa in input char s; // carattere corrente della stringa in input int num_unmatched_open = 0, num_unmatched_closed = 0; ifstream fin("input.txt"); assert( fin ); fin >> n; for(int i = 0; i < n; i++) { fin >> s; if( s == '(' ) // s[i] == '(' num_unmatched_open++; else { // s[i] == ')' if(num_unmatched_open > 0) num_unmatched_open--; else num_unmatched_closed++; } } fin.close(); ofstream fout("output.txt"); fout << half_rounded_up(num_unmatched_closed) + half_rounded_up(num_unmatched_open) << endl; fout.close(); return 0; }