此题可以用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. 用Kotlin开发android平台语音识别,语义理解应用(olamisdk)

    Kotlin是由JetBrains创建的基于JVM的编程语言,IntelliJ正是JetBrains的杰作,而android Studio是 基于IntelliJ修改而来的.Kotlin是一门包含很多 ...

  2. JavaScript调试技巧之console.log()详解--2015-08-07

    对于JavaScript程序的调试,相比于alert(),使用console.log()是一种更好的方式,原因在于:alert()函数会阻断 JavaScript程序的执行,从而造成副作用:而cons ...

  3. 记服务器 httpd 服务无法启动

    httpd 服务无法重启 ,网上查到的资料都是端口号冲突 ,记录一下自己的解决办法 !!! 首先查看 服务状态 :  service status httpd / systemctl status h ...

  4. 小型LAMP搭建

    一.dns的搭建 安装dns服务 yum install bind 修改dns的主配置文件 [root@234c17 named]# vim /etc/named.conf // // named.c ...

  5. 快速入门Pandas

    教你十分钟学会使用pandas. pandas是python数据分析的一个最重要的工具. 基本使用 # 一般以pd作为pandas的缩写 import pandas as pd # 读取文件 df = ...

  6. PAT Basic 1023

    1023 组个最小数 给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小 ...

  7. 7,数据类型转换,set 集合,和深浅copy

    str转换成list  用split list转换成str  用join tuple转换成list tu1 = (1,2,3) li = list(tu1)(强转) tu2 = tuple(li)(强 ...

  8. luogu1129 [ZJOI2007]矩阵游戏

    其实,只用考虑某一行能否放到某一行就行了 #include <iostream> #include <cstring> #include <cstdio> usin ...

  9. 大数据学习——Kafka集群部署

    1下载安装包 2解压安装包 -0.9.0.1.tgz -0.9.0.1 kafka 3修改配置文件 cp server.properties  server.properties.bak # Lice ...

  10. unittest编写Web测试用例

    案例:百度搜索关键词:“unittest” test_baidu.py: from selenium import webdriver from time import sleep import un ...