此题可以用STL的multiset解决,也可以手打一棵伸展树(Splay)来求前驱与后驱。

使用multiset:

#include<iostream>
#include<set>
#include<algorithm>
#include<stdio.h>
using namespace std; typedef long long LL; multiset<LL> se; /*
LL abs(LL a)
{
return a > 0 ? a : -a;
}*/ int main()
{
int n;
scanf("%d", &n);
LL a, ans = 0;
multiset<LL>::iterator it, tmp1,tmp2;
for (int i = 0; i < n; i++)
{
if (scanf("%lld", &a) == EOF) a = 0;
se.insert(a);
if (i == 0)
{
ans = a;
continue;
}
it = se.find(a);
tmp1 = tmp2 = it;
if (it == se.begin())
{
it++;
ans += (*it - a);
continue;
}
tmp2++;
if (tmp2 == se.end())
{
it--;
ans += (a - *it);
continue;
}
tmp1--;
ans += min(a - *tmp1, *tmp2 - a); }
printf("%lld\n", ans);
return 0;
}

使用Splay的代码:

#include<iostream>
#include<algorithm>
#define leftSon ch[0]
#define rightSon ch[1]
using namespace std; struct node
{
node *father, *ch[2];
int key, sam;
node(){ sam = 0; }
}; node *root;
node R;
bool flag = 1; void Rotate(node *now, int c)
{
node *y = now->father;
y->ch[!c] = now->ch[c];
if (now->ch[c] != NULL)
now->ch[c]->father = y;
now->father = y->father;
if (y->father != NULL)
if (y->father->ch[0] == y)
y->father->ch[0] = now;
else y->father->ch[1] = now;
now->ch[c] = y, y->father = now;
if (y == root) root = now;
} void Splay(node *now, node *f) // Splay
{
for (; now->father != f;)
if (now->father->father == f)
if (now->father->ch[0] == now)
Rotate(now, 1);
else Rotate(now, 0);
else
{
node *y = now->father, *z = y->father;
if (z->ch[0] == y)
if (y->ch[0] == now)
Rotate(y, 1), Rotate(now, 1);
else
Rotate(now, 0), Rotate(now, 1);
else if (y->ch[1] == now)
Rotate(y, 0), Rotate(now, 0);
else
Rotate(now, 1), Rotate(now, 0);
}
} void ini()
{
root = &R;
root->father = root->ch[0] = root->ch[1] = NULL;
} void Insert(int x, node *now, node *pre)
{
if (now != NULL&&now->key == x){ now->sam++; return; }
if (now == root&&flag)
{
now->key = x;
now->rightSon = NULL;
now->leftSon = NULL;
now->father = pre;
now->sam++;
flag = 0;
return;
}
if (now == NULL)
{
node *newNode;
newNode = new node;
newNode->key = x;
newNode->rightSon = NULL;
newNode->leftSon = NULL;
newNode->father = pre;
newNode->sam = 1;
if (x > pre->key)
pre->rightSon = newNode;
else
pre->leftSon = newNode;
Splay(newNode, NULL);
return;
}
if (x > now->key)
Insert(x, now->rightSon, now);
else
Insert(x, now->leftSon, now);
} node* find(int x, node *now)
{
if (now == NULL)
return NULL;
if (now->key == x)
return now;
if (x < now->key)
return find(x, now->ch[0]);
else
return find(x, now->ch[1]);
return NULL;
} node* searchMin(node *now)
{
if (now == NULL)
return NULL;
if (now->leftSon == NULL)
return now;
searchMin(now->leftSon);
} node* searchMax(node *now)
{
if (now == NULL)
return NULL;
if (now->rightSon == NULL)
return now;
searchMax(now->rightSon);
} void Delete(int x)
{
node *now = find(x, root);
if (now == NULL)
return;
if (now->key == x)
{
if (now->leftSon == NULL&&now->rightSon == NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = NULL;
else
now->father->rightSon = NULL;
return;
}
if (now->leftSon == NULL&&now->rightSon != NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = now->rightSon;
else
now->father->rightSon = now->rightSon;
now->rightSon->father = now->father;
return;
}
if (now->leftSon != NULL&&now->rightSon == NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = now->leftSon;
else
now->father->rightSon = now->leftSon;
now->leftSon->father = now->father;
return;
}
node *tmp = searchMin(now->rightSon);
now->key = tmp->key;
now->sam = tmp->sam;
if (tmp->father->leftSon == tmp)
tmp->father->leftSon = NULL;
else
tmp->father->rightSon = NULL;
return;
}
} void middleTravel(node *now)
{
if (now == NULL)
return;
middleTravel(now->leftSon);
for (int i = 0; i < (now->sam); i++)
cout << now->key << " ";
middleTravel(now->rightSon);
} int main()
{
ini();
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++)
{
int a;
if (!(cin >> a))a = 0;
if (i == 0)
{
ans = a;
Insert(a, root, NULL);
continue;
}
node *tmp = find(a, root);
if (tmp != NULL)
{
Insert(a, root, NULL);
continue;
}
Insert(a, root, NULL);
tmp = find(a, root);
node *pre = searchMax(tmp->ch[0]);
node *nex = searchMin(tmp->ch[1]);
if (pre == NULL&&nex != NULL)
ans += nex->key - a;
else if (pre != NULL&&nex == NULL)
ans += a - pre->key;
else if (pre != NULL&&nex != NULL)
ans += min(a - pre->key, nex->key - a);
}
cout << ans << endl;
return 0;
}

HNOI_2002 营业额统计(Splay)的更多相关文章

  1. NOI 2002 营业额统计 (splay or fhq treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  2. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  3. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  4. NOIP 营业额统计 splay tree 纯模板

    2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 389       ...

  5. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  6. 洛谷P2234 [HNOI2002] 营业额统计 [splay]

    题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  7. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

  8. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  9. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

  10. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

随机推荐

  1. 浅谈倍增LCA

    题目链接:https://www.luogu.org/problemnew/show/P3379 刚学了LCA,写篇blog加强理解. LCA(Least Common Ancestors),即最近公 ...

  2. MYSQL - 限制资源的使用

    MYSQL - 限制资源的使用 1.MAX_QUERIES_PER_HOUR 用来限制用户每小时运行的查询数量 mysql> grant select on *.* to 'cu_blog'@' ...

  3. vuejs 的错误代码,有助于理解

    1.vuejs 的双向绑定理解 2.嵌套复杂对象的使用. * 1. 分割都是, 2. json 中分割都是; 4. v-bind:value="param" 括号内的就是 vuej ...

  4. ios面试题(三)

    4.写一个setter方法用于完成@property (nonatomic,retain)NSString *name,写一个setter方法用于完成@property(nonatomic,copy) ...

  5. NOIP 2018数据点

    链接: https://pan.baidu.com/s/14jXQGPSac3b9_m5h5x2wGQ 提取码: 1cbk 好文别忘点赞!!!

  6. logging模块,程序日志模板

    6.11自我总结 1.logging模块 用于程序的运行日志 1.初级 #首先程序运行分会出现5中情况 1.logging.info('info') #程序正常运行级别为10 2.logging.de ...

  7. 【C#】【数据结构】002-线性表:单链表

    C#数据结构:单链表 1.自定义单链表结构: 单链节点类 using System.Collections; using System.Collections.Generic; using Unity ...

  8. gitlab之gitlab-ci自动部署

    简介 gitlab-ci全称是gitlab continuous integration的意思,也就是持续集成.中心思想是当每一次push到gitlab的时候,都会触发一次脚本执行,然后脚本的内容包括 ...

  9. Exchange 正版化 授权

    网友说法: Exchange服务器版其实价格不高,企业版也就是几万左右,贵的是客户端授权,一个客户端授权大概要300多.但是,但是,中国企业买Exchange客户端一般都是可以按比例买的,比如10%- ...

  10. Python 编程要求

    1.添加前缀 #!/usr/bin/env python # -*- coding:utf-8 -*- 2.py文件.函数都要写好注释 3.主函数要加上判断 if __name__ == " ...