营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。 输入输出要求
Input
第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i
天公司的营业额。
天数n<=32767,
每天的营业额ai <= 1,000,000。
最后结果T<=2^31
Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。
Sample Input
6
5
1
2
5
4
6
Sample Output
12
Hint
结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
splay 求其前驱及后继;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 1e9 + 7;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;
inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
int f[maxn], ch[maxn][2], key[maxn], cnt[maxn], Size[maxn];
int sz, root;
// ch[i][1]--> i 右儿子; ch[i][0]--> i 左儿子
int n;
inline void clear(int x) {
ch[x][0] = ch[x][1] = f[x] = key[x] = cnt[x] = Size[x] = 0;
}
// 清零
inline int get(int x) {
return ch[f[x]][1] == x;
}
// 判断当前点是它父亲的左儿子还是右儿子;
inline void update(int x) {
if (x) {
Size[x] = cnt[x];
if (ch[x][0])Size[x] += Size[ch[x][0]];
if (ch[x][1])Size[x] += Size[ch[x][1]];
}
}
// 更新当前点的size 值
inline void rotate(int x) {
int old = f[x], oldf = f[old], which = get(x);
ch[old][which] = ch[x][which ^ 1];
f[ch[old][which]] = old;
f[old] = x; ch[x][which ^ 1] = old;
f[x] = oldf;
if (oldf) {// rotate 后的位置不是根
ch[oldf][ch[oldf][1] == old] = x;
}
update(old); update(x);
}
inline void splay(int x) {
for (int fa; (fa = f[x]); rotate(x)) {
if (f[fa]) {
rotate((get(x) == get(fa) ? fa : x));
}
}
root = x;
}
// 插入
inline void insert(int v) {
if (root == 0) {
sz++; ch[sz][0] = ch[sz][1] = f[sz] = 0;
key[sz] = v; cnt[sz] = 1; Size[sz] = 1; root = sz; return;
}
int now = root, fa = 0;
while (1) {
if (key[now] == v) {
cnt[now]++; update(now); update(fa); splay(now); break;
}
fa = now; now = ch[now][key[now] < v];
if (now == 0) {
sz++; ch[sz][0] = ch[sz][1] = 0; key[sz] = v; Size[sz] = 1;
cnt[sz] = 1; f[sz] = fa; ch[fa][key[fa] < v] = sz;
update(fa); splay(sz); break;
}
}
}
// find 操作,找 x 的排名
inline int Find(int v) {
int ans = 0, now = root;
while (1) {
if (v < key[now])now = ch[now][0];
else {
ans += (ch[now][0] ? Size[ch[now][0]] : 0);
if (v == key[now]) {
splay(now); return ans + 1;
}
ans += cnt[now]; now = ch[now][1];
}
}
}
// 找到排名为 x 的点;
inline int findx(int x) {
int now = root;
while (1) {
if (ch[now][0] && x <= Size[ch[now][0]]) {
now = ch[now][0];
}
else {
int tmp = (ch[now][0] ? Size[ch[now][0]] : 0) + cnt[now];
if (x <= tmp) {
return key[now];
}
x -= tmp; now = ch[now][1];
}
}
}
// 求 x 的前驱(< x 的最大的数)或后继(> x 的最小的数)
inline int pre() {
int now = ch[root][0];
while (ch[now][1])now = ch[now][1];
return now;
}
inline int nxt() {
int now = ch[root][1];
while (ch[now][0])now = ch[now][0];
return now;
}
// 删除操作
inline void del(int x) {
int wt = Find(x);
if (cnt[root] > 1) {
cnt[root] --; update(root); return;
}
if (!ch[root][0] && !ch[root][1]) { clear(root); root = 0; return; }
if (!ch[root][0]) {
int oldroot = root; root = ch[root][1]; f[root] = 0; clear(oldroot);
return;
}
else if (!ch[root][1]) {
int oldroot = root; root = ch[root][0]; f[root] = 0; clear(oldroot);
return;
}
int leftbig = pre(), oldroot = root;
splay(leftbig);
f[ch[oldroot][1]] = root;
ch[root][1] = ch[oldroot][1];
clear(oldroot); update(root); return;
}
map<int, int>mp;
int main()
{
//ios::sync_with_stdio(false);
rdint(n);
int ans = 0;
for (int i = 1; i <= n; i++) {
int minn = inf;
int v; rdint(v); insert(v);
mp[v]++;
// cout << cnt[v] << endl;
int pp = pre(), nn = nxt();
if (i == 1) {
ans += abs(v);
}
else {
// minn = min(abs(v - key[pp]), abs(v - key[nn]));
// ans += minn;
// cout << pp << ' ' << nn << endl;
// cout << key[pp] << ' ' << key[nn] << endl;
if (mp[v] > 1)ans += 0;
else {
if (pp != 0 && nn != 0) {
ans += min(abs(v - key[pp]), abs(v - key[nn]));
// cout << min(abs(v - key[pp]), abs(v - key[nn])) << endl;
}
else {
if (pp != 0)ans += abs(v - key[pp]);// cout << abs(v - key[pp]) << endl;
else if (nn != 0)ans += abs(v - key[nn]);// cout << abs(v - key[nn]) << endl;
}
}
}
}
cout << ans << endl;
return 0;
}