Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : 12572 - RMQ Overkill
Source : UVa Online Judge
Category : Data Structure
Algorithm : Histogram Technique
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- static const int maxn = 1e4 + 5;
- static const int mod = 1000000007;
-
- int arr[maxn];
- int Left[maxn], Right[maxn];
-
- int main()
- {
- int n;
- char s[maxn];
- while (scanf("%d", &n) == 1)
- {
- scanf("%s", s);
- for (int i = 0; i < n; i++) arr[i+1] = s[i] - '0';
- for (int i = 1; i <= n; i++)
- {
- int cnt = 0;
- int r = i;
- while (r <= n && arr[r] >= arr[i]) cnt++, r++;
- Right[i] = cnt;
- cnt = 0;
- int l = i-1;
- while (l >= 1 && arr[l] > arr[i]) cnt++, l--;
- Left[i] = (Right[i] * cnt);
- }
- int sum = 0;
- for (int i = 1; i <= n; i++)
- {
- sum += (arr[i] * (Left[i] + Right[i]));
- if (sum > mod) sum -= mod;
- }
- printf("%d\n", sum);
- }
- }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.