Advertisement
Nita_Cristian

expresie7

Feb 25th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define pr 10000005
  4. #define pd 10000005
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("expresie7.in");
  9. ofstream fout("expresie7.out");
  10.  
  11. char s[100005];
  12. int n, numere;
  13.  
  14. int rezolvare(int &suma)
  15. {
  16.     stack<int> sv;
  17.  
  18.     for(int i = 0; i < n; i++)
  19.     {
  20.         if(s[i] == ',')
  21.             continue;
  22.         if(isdigit(s[i]) || s[i] == '-')
  23.         {
  24.             sv.push(atoi(s+i));
  25.             i += strspn(s+i, "0123456789-");
  26.             numere++;
  27.         }
  28.         if(s[i] == '(')
  29.         {
  30.             sv.push(pr);
  31.         }
  32.         if(s[i] == '[')
  33.         {
  34.             sv.push(pd);
  35.         }
  36.         if(s[i] == ')')
  37.         {
  38.             int maxim = 0, total = INT_MIN;
  39.             while(sv.top() != pr)
  40.             {
  41.                 maxim = max(sv.top(), maxim + sv.top());
  42.                 total = max(maxim, total);
  43.                 sv.pop();
  44.             }
  45.             sv.pop();
  46.             sv.push(total);
  47.         }
  48.         if(s[i] == ']')
  49.         {
  50.             vector<int> v;
  51.             while(sv.top() != pd)
  52.             {
  53.                 v.push_back(sv.top());
  54.                 sv.pop();
  55.             }
  56.             sv.pop();
  57.  
  58.             nth_element(v.begin(), v.begin() + (v.size()-1)/2, v.end());
  59.             sv.push(v[(v.size()-1)/2]);
  60.         }
  61.     }
  62.     suma = 0;
  63.     while(!sv.empty())
  64.     {
  65.         suma += sv.top();
  66.         sv.pop();
  67.     }
  68. }
  69.  
  70. int main()
  71. {
  72.     fin.getline(s, 100005);
  73.     n = strlen(s);
  74.  
  75.     int s = 0;
  76.     rezolvare(s);
  77.     fout << numere << '\n' << s;
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement