Advertisement
a53

leftmax

a53
Mar 11th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /// sursa oficiala 100p O(N)
  2. /// student Tamio-Vesa Nakajima
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. using ll = long long;
  7. using cd = complex<double>;
  8.  
  9. int main(){
  10. ifstream f("leftmax.in");
  11. ofstream g("leftmax.out");
  12.  
  13. // Citim inputul.
  14. int n;
  15. f >> n;
  16. vector<int> v(n);
  17. for(auto& x : v)
  18. f >> x;
  19.  
  20. // Folosim o stiva pentru a calcula sirurile
  21. // st si dr din solutie.
  22. vector<int> st(n), dr(n);
  23. stack<int> stiva;
  24. for(int i = 0; i < n; ++i){
  25. while(!stiva.empty() && v[stiva.top()] < v[i]){
  26. dr[stiva.top()] = i - 1;
  27. stiva.pop();
  28. }
  29.  
  30. st[i] = (stiva.empty() ? 0 : stiva.top() + 1);
  31. stiva.push(i);
  32. }
  33. while(!stiva.empty()){
  34. dr[stiva.top()] = n - 1;
  35. stiva.pop();
  36. }
  37.  
  38. // Acum vom itera prin sir, retinand solutia in ret.
  39. ll ret = 0;
  40. for(int i = 0; i < n; ++i){
  41. const ll L = i - st[i], R = dr[i] - i;
  42. ret += (min(R, L) + 1) * (R - L + 2 + max(R, L)) / 2;
  43. }
  44.  
  45. // Afisam solutia.
  46. g << ret % 1000000007 << endl;
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement