A8543 | Range Increments
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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:
Before calls of function rangeIncrement(l, r) all array elements equal zero.
<br></br>function rangeIncrement(l, r)<br></br> for i := l .. r do<br></br> a[i] = a[i] + 1<br></br>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 $10^{5}$ .Before calls of function rangeIncrement(l, r) all array elements equal zero.
输入格式
The first input line contains a single integer $n$ ( $1<=n<=10^{5}$ ) — 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]<=10^{5}$ ) 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 $10^{5}$ calls of function rangeIncrement(l, r).
The second line contains its integer space-separated elements, $a[1],a[2],...,a[n]$ ( $0<=a[i]<=10^{5}$ ) 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 $10^{5}$ 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 $10^{5}$ .
Then print $t$ lines — the descriptions of function calls, one per line. Each line should contain two integers $l_{i},r_{i}$ ( $1<=l_{i}<=r_{i}<=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.
Then print $t$ lines — the descriptions of function calls, one per line. Each line should contain two integers $l_{i},r_{i}$ ( $1<=l_{i}<=r_{i}<=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.
输入输出样例
输入 #1
6 1 2 1 1 4 1
输出 #1
5 2 2 5 5 5 5 5 5 1 6
输入 #2
5 1 0 1 0 1
输出 #2
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).
- 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).
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted