题解
- 定义一个k,表示当前i的值
- 那么每次比较a[i]与k
- 如果a[i]>k,则打入a[i]-k个+1标记
- 如果a[i]<k,则打入k-a[i]个-1标记,并且与+1标志匹配
代码
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<algorithm>
5 using namespace std;
6 int n,ans,z;
7 int a[100005],l[100005],r[100005];
8 int main()
9 {
10 freopen("range.in","r",stdin);
11 freopen("range.out","w",stdout);
12 scanf("%d",&n);
13 for(int i=1;i<=n;i++) scanf("%d",&a[i]);
14 int k=0;
15 for(int i=1;i<=n+1;i++)
16 {
17 while(a[i]>k) k++,l[++ans]=i;
18 while(a[i]<k) k--,r[++z]=i-1;
19 }
20 printf("%d
",ans);
21 for(int i=1;i<=ans;i++) printf("%d %d
",l[i],r[i]);
22 return 0;
23 }