此题可以用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. thinkphp 结合phpexcel实现excel导入

    控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...

  2. java获取本地计算机MAC地址

    java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...

  3. linux-ngnix服务(一)

    httpd MPM: prefork:进程模型,两级结构,主进程master负责生成子进程,每个子进程负责响应一个请求 worker:线程模型,三级结构,主进程master负责生成子进程,每个子进程负 ...

  4. back to back

    back to back 传输,以前在AMBA bus中遇到过,FIFO设计中再次遇到. 查了资料大概意思是:直接传输,不依靠中介,连续多次传输.

  5. 数据结构( Pyhon 语言描述 ) — — 第8章:队列

    队列概览 队列是线性的集合 队列的插入限制在队尾,删除限制在队头.支持先进先出协议( FIFIO, first-in first-out ) 两个基本操作 add:在队尾添加一项 pop:从队头弹出一 ...

  6. PHP-redis命令之 散列(hashes)

    二.散列(hashes) 1.hset:设置散列的值 $redis->hset('myhas','field1','hello'); $redis->hset('myhas','field ...

  7. 【Kubernetes】Deployment控制器模型

    在Kubernetes中,Deployment是最基本的控制器对象 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deploym ...

  8. redis2.3.7安装时出现undefined reference to `clock_gettime'

    (转自:http://blog.csdn.net/qq_28779503/article/details/54844988) undefined reference to `clock_gettime ...

  9. 手写数字0-9的识别代码(SVM支持向量机)

    帮一个贴吧的朋友改的一段代码,源代码来自<机器学习实战> 原代码的功能是识别0和9两个数字 经过改动之后可以识别0~9,并且将分类器的产生和测试部分分开来写,免得每次测试数据都要重新生成分 ...

  10. UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree

    魔法森林高清重置, 只需要维护关于t的最大生成树,然后链上边权求和即可. 直接上LCT 调了将近2h 吃枣药丸 #include <cstdio> #include <cstring ...