题目:
给定一个长度为N的序列A1到AN, 求所有区间[L, R] (1<=L<=R<=N)的极差之和,其中区间[L, R]的极差定位为A1到AR中最大值与最小值之差
输入:
第一行包含一个整数N,1<=N<=10^5
第二行包含N个空格隔开的整数A1到AN,1<=Ai<=10^5
输出:
输出所有区间的极差之和
样例输入:
5
4 1 8 2 5
样例输出:
60
代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long n;
cin >> n;
vector<long> m;
for (long i = 0; i < n; ++i)
{
long num;
cin >> num;
m.push_back(num);
}
long long sum = 0;
for (long i = 0; i < n; ++i)
{
long Max = m[i];
long Min = m[i];
for (long j = i + 1; j < n; ++j)
{
Max = max(Max, m[j]);
Min = min(Min, m[j]);
sum += (Max - Min);
}
}
cout << sum << endl;
return 0;
}