Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll a[2000012], dp[2110000], dp1[2000101];
  5. int main() {
  6. stack <ll> q, q1;
  7. int n;
  8. cin >> n;
  9. for (int i = 0; i < n; i++) {
  10. cin >> a[i];
  11. }
  12. for (int i=0; i < n; i++) {
  13. if (q.size() == 0) {
  14. q.push(i);
  15. continue;
  16. }
  17. ll k = q.top();
  18. if (a[i] <= a[k]) {
  19. while (!q.empty()) {
  20. if (a[q.top()] < a[i]) {
  21. break;
  22. }
  23. dp[i]+= dp[q.top()] + 1;
  24. q.pop();
  25. }
  26. q.push(i);
  27. } else {
  28. while (!q.empty()) {
  29. if (a[q.top()] < a[i]) {
  30. break;
  31. }
  32. dp[i]+= dp[q.top()] + 1;
  33. q.pop();
  34. }
  35. q.push(i);
  36. }
  37. }
  38. for (int i = n - 1; i >= 0; i--) {
  39. if (q1.size() == 0) {
  40. q1.push(i);
  41. continue;
  42. }
  43. ll k = q1.top();
  44. if (a[i] <= a[k]) {
  45. while (!q1.empty()) {
  46. if (a[q1.top()] < a[i]) {
  47. break;
  48. }
  49. dp1[i]+= dp1[q1.top()] + 1;
  50. q1.pop();
  51. }
  52. q1.push(i);
  53. } else {
  54. while (!q1.empty()) {
  55. if (a[q1.top()] < a[i]) {
  56. break;
  57. }
  58. dp1[i]+= dp1[q1.top()] + 1;
  59. q1.pop();
  60. }
  61. q1.push(i);
  62. }
  63. }
  64. ll area = -1, l, r;
  65. for (int i = 0; i < n; i++) {
  66. int l1 = i - dp[i], r1 = dp1[i] + i;
  67. if (area < (dp[i] + dp1[i] + 1) * a[i]
  68. || area == (dp[i] + dp1[i] + 1) * a[i] && r - l +1 >r1 - l1 + 1) {
  69. area = (dp[i] + dp1[i] + 1) * a[i];
  70. l = i - dp[i];
  71. r = i + dp1[i];
  72. }
  73. if (area <= a[i]) {
  74. area = a[i];
  75. l = i;
  76. r = i;
  77. }
  78. }
  79. cout << area << endl << l + 1 << ' ' << r + 1;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement