Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pr 10000005
- #define pd 10000005
- using namespace std;
- ifstream fin("expresie7.in");
- ofstream fout("expresie7.out");
- char s[100005];
- int n, numere;
- int rezolvare(int &suma)
- {
- stack<int> sv;
- for(int i = 0; i < n; i++)
- {
- if(s[i] == ',')
- continue;
- if(isdigit(s[i]) || s[i] == '-')
- {
- sv.push(atoi(s+i));
- i += strspn(s+i, "0123456789-");
- numere++;
- }
- if(s[i] == '(')
- {
- sv.push(pr);
- }
- if(s[i] == '[')
- {
- sv.push(pd);
- }
- if(s[i] == ')')
- {
- int maxim = 0, total = INT_MIN;
- while(sv.top() != pr)
- {
- maxim = max(sv.top(), maxim + sv.top());
- total = max(maxim, total);
- sv.pop();
- }
- sv.pop();
- sv.push(total);
- }
- if(s[i] == ']')
- {
- vector<int> v;
- while(sv.top() != pd)
- {
- v.push_back(sv.top());
- sv.pop();
- }
- sv.pop();
- nth_element(v.begin(), v.begin() + (v.size()-1)/2, v.end());
- sv.push(v[(v.size()-1)/2]);
- }
- }
- suma = 0;
- while(!sv.empty())
- {
- suma += sv.top();
- sv.pop();
- }
- }
- int main()
- {
- fin.getline(s, 100005);
- n = strlen(s);
- int s = 0;
- rezolvare(s);
- fout << numere << '\n' << s;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement