此题可以用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. 初识 Hibernate

    Hibernate 框架 1.1   什么是框架? 框架是一个提供了可重用的公共结构半成品. 2.1   关于Hibernate Hibernate是数据持久层的一个轻量级框架.数据持久层的框架有很多 ...

  2. Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作

    查询目标是完成3个功能: (1)查询表,返回某一个值.例如查询表中记录的条数,返回一个int类型数据 (2)查询表,返回结果为某一个对象. (3)查询表,返回结果为某一个泛型的list集合. 一.查询 ...

  3. CF-1082(渣渣只做了前三个)

    链接:http://codeforces.com/contest/1082 A. Vasya and Book 题意: n,x,y,d 一本电子书有n页,每一次翻动只能往前或者往后翻d页.求x-> ...

  4. [LUOGU] 1002 过河卒

    题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为"马拦过 ...

  5. opencast的docker安装

    在之前的从源安装和从包安装opencast,都遇到较多环境问题导致失败.所有采用docker安装. Dockers是有能力打包应用程序及其虚拟容器,可以在任何Linux服务器上运行的依赖性工具,这有助 ...

  6. Vue—事件修饰符

    Vue事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation(). Vue. ...

  7. PyCharm学习笔记(二) 调试配置

    选择PyCharm编译器 注意工程默认使用的解释器可能是Pycharm自带的,而不是单独安装的.

  8. u-boot顶层Makefile分析

    1.u-boot制作命令 make forlinx_nand_ram256_config: make all; 2.顶层mkconfig分析,参考 U-BOOT顶层目录mkconfig分析 mkcon ...

  9. Python Jquery学习

    jquery调用方法: $(css的选择器).操作函数 语法格式: 操作函数: html      修改内容 点击button键后,jquery就会变为bootstrap 当然里面也可以进行判断,实现 ...

  10. Java-获取Class对象的名称

    package com.tj; public class MyClass2 { public static void main(String[] args) { Class cls = java.la ...