Algorithm
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following:
function rangeIncrement(l, r)
for i := l .. r do
a[i] = a[i] + 1
Polycarpus knows the state of the array a after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.
Before calls of function rangeIncrement(l, r) all array elements equal zero.
The first input line contains a single integer n (1 ≤ n ≤ 105) — the length of the array a[1... n].
The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 ≤ a[i] ≤ 105) after some series of function calls rangeIncrement(l, r).
It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).
Print on the first line t — the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.
Then print t lines — the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 ≤ li ≤ ri ≤ n) — the arguments of the i-th call rangeIncrement(l, r). Calls can be applied in any order.
If there are multiple solutions, you are allowed to print any of them.
6
1 2 1 1 4 1
5
2 2
5 5
5 5
5 5
1 6
5
1 0 1 0 1
3
1 1
3 3
5 5
The first sample requires a call for the entire array, and four additional calls:
- one for the segment [2,2] (i.e. the second element of the array),
- three for the segment [5,5] (i.e. the fifth element of the array).
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, s, e, val, a[N], seg[N * 4];
vector<pair<int, int> > sol;
queue<pair<pair<int, int>, int> > q;
vector<vector<int> > pos;
void build(int at, int l, int r) {
if(l == r) {
seg[at] = a[l];
return;
}
int mid = (l + r) >> 1;
build(at << 1, l, mid);
build(at << 1 | 1, mid + 1, r);
seg[at] = min(seg[at << 1], seg[at << 1 | 1]);
}
int get(int at, int l, int r) {
if(l > e || r < s)
return 1e9;
if(l >= s && r <= e)
return seg[at];
int mid = (l + r) >> 1;
return min(get(at << 1, l, mid), get(at << 1 | 1, mid + 1, r));
}
int main() {
pos.resize(N);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", a + i);
pos[a[i]].push_back(i);
}
build(1, 1, n);
q.push({{1, n}, 0});
while(!q.empty()) {
int l = q.front().first.first, r = q.front().first.second;
int cur = q.front().second;
q.pop();
if(!(l <= r))
continue;
s = l, e = r;
int mn = get(1, 1, n);
for(int i = 0; i < mn - cur; ++i)
sol.push_back({l, r});
int idx = lower_bound(pos[mn].begin(), pos[mn].end(), l) - pos[mn].begin();
q.push({{l, pos[mn][idx] - 1}, (mn - cur) + cur});
++idx;
while(idx < pos[mn].size() && pos[mn][idx] <= r) {
q.push({{pos[mn][idx - 1] + 1, pos[mn][idx] - 1}, (mn - cur) + cur});
++idx;
}
--idx;
q.push({{pos[mn][idx] + 1, r}, (mn - cur) + cur});
}
printf("%d\n", int(sol.size()));
for(int i = 0; i < sol.size(); ++i)
printf("%d %d\n", sol[i].first, sol[i].second);
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 2 1 1 4 1
Output
2 2
5 5
5 5
5 5
1 6
Demonstration
Codeforcess Solution Range Increments, C. Range Increments ,C,C++, Java, Js and Python ,Range Increments,Codeforcess Solution