5812. 【NOIP提高A组模拟2018.8.14】 区间
(File IO): input:range.in output:range.out
Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Special Judge Downloads
Goto ProblemSet做法:一开始还想用线段树来着,后来发现会超时,题解上说打差分从头到尾扫一遍就好啦
代码如下:
View Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define N 100007 5 using namespace std; 6 int n, a[N], list[N], tot, ans; 7 8 int abs (int x) { return x > 0 ? x : -x; } 9 10 void Work(int step, int Times) 11 { 12 for (; Times--; list[++tot] = step); 13 } 14 15 void Print(int step, int Times) 16 { 17 for(; Times--; printf("%d %d ", list[tot--], step)); 18 } 19 20 int main() 21 { 22 freopen("range.in", "r", stdin); 23 freopen("range.out", "w", stdout); 24 scanf("%d", &n); 25 for (int i = 1; i <= n; scanf("%d", &a[i++])); 26 for (int i = 1; i <= n; i++) 27 if (a[i] > a[i - 1]) ans += a[i] - a[i - 1]; 28 printf("%d ", ans); 29 for (int i = 1; i <= n + 1; i++) 30 { 31 if (a[i] > a[i - 1]) Work(i, abs(a[i] - a[i - 1])); 32 if (a[i] < a[i - 1]) Print(i - 1, abs(a[i] - a[i - 1])); 33 } 34 }