1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 5783 Solved: 1859
[Submit][Status]
Description
Input
Output
Sample Input
5
1
2
5
4
6
Sample Output
HINT
结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
Source
/* ***********************************************
Author :kuangbin
Created Time :2013/8/24 10:57:20
File Name :F:\2013ACM练习\专题学习\splay_tree_2\营业额统计.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; #define Key_value ch[ch[root][1]][0]
const int MAXN = ;
int pre[MAXN],ch[MAXN][],key[MAXN];
int root,tot1; int s[MAXN],tot2;//内存池 void NewNode(int &r,int father,int k)
{
if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就要是++tot2
else r = ++tot1;//这个必须是++tot1
pre[r] = father;
ch[r][] = ch[r][] = ;
key[r] = k;
}
//初始化
void Init()
{
root = tot1 = tot2 = ;
ch[root][] = ch[root][] = key[root] = pre[root] = ;
}
//旋转,0为左旋,1为右旋
void Rotate(int x,int kind)
{
int y = pre[x];
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
}
//Splay调整,将r结点调整到goal下面
void Splay(int r,int goal)
{
while(pre[r] != goal)
{
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][] ==r);
else
{
int y = pre[r];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
if(goal == )root = r;
} void Insert(int k)//插入一个值为k的结点(有重复插入)
{
int r = root;
if(r == )
{
NewNode(root,,k);
return;
}
while(ch[r][key[r]<k])
r = ch[r][key[r]<k];
NewNode(ch[r][key[r]<k],r,k);
Splay(ch[r][key[r]<k],);//转到根部
}
//找前驱
int Get_pre(int r)
{
if(ch[r][] == )return -;//不存在
r = ch[r][];
while(ch[r][])r = ch[r][];
return r;
}
//找后继
int Get_next(int r)
{
if(ch[r][] == )return -;
r = ch[r][];
while(ch[r][])r = ch[r][];
return r;
}
const int INF = 0x3f3f3f3f; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n) == )
{
Init();
int a;
int ans = ;
for(int i = ;i < n;i++)
{
if(scanf("%d",&a) == EOF) a= ;
Insert(a);
if(i == )
ans += a;
else
{
int tmp = INF;
int t1 = Get_pre(root);
if(t1 != -)
tmp = min(tmp,key[root] - key[t1]);
int t2 = Get_next(root);
if(t2 != -)
tmp = min(tmp,key[t2] - key[root]);
ans += tmp;
}
}
printf("%d\n",ans);
}
return ;
}
1588: [HNOI2002]营业额统计 (splay tree)的更多相关文章
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- [HNOI2002]营业额统计 Splay tree
Splay tree入门题,学好代码风格,学习HH大牛的,传送门.. #include <functional> #include <algorithm> #include & ...
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...
- BZOJ 1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14396 Solved: 5521[Submit][Sta ...
- bzoj 1588: [HNOI2002]营业额统计 treap
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 13902 Solved: 5225[Submit][Sta ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
随机推荐
- git内部原理
Git 内部原理 无论是从之前的章节直接跳到本章,还是读完了其余章节一直到这——你都将在本章见识到 Git 的内部工作原理 和实现方式. 我们发现学习这部分内容对于理解 Git 的用途和强大至关重要. ...
- show engine innodb status 详细介绍
Contents Header1 SEMAPHORES. 1 LATEST DETECTED DEADLOCK. 3 TRANSACTIONS. 5 什么是purge操作... 5 FILE I/O. ...
- 读书笔记--C陷阱与缺陷(三)
第三章 1. 指针与数组 书中强调C中数组注意的两点: 1) C语言只有一维数组,但是数组元素可以是任何类型对象,是另外一个数组时就产生了二维数组.数组大小是常数(但GCC实现了变长数组..) ...
- Android Studio配置opencv
安装过程参考:http://www.cnblogs.com/tail/p/4618476.html demo参考:http://blog.csdn.net/gao_chun/article/detai ...
- kafka基本版与kafka acl版性能对比(单机版)
一.场景 线上已经有kafka集群,服务运行稳定.但是因为产品升级,需要对kakfa做安全测试,也就是权限验证. 但是增加权限验证,会不会对性能有影响呢?影响大吗?不知道呀! 因此,本文就此来做一下对 ...
- Vue-Socket.io
github地址:https://github.com/MetinSeylan/Vue-Socket.io 安装: npm install vue-socket.io -S 注册: import Vu ...
- CCF CSP 201409-4 最优配餐
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201409-4 最优配餐 问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越 ...
- mysql热数据加载管理
5.6版本之后,提供了一个新特性来快速预热buffer_pool缓冲池.在my.cnf里面加入几个参数: innodb_buffer_pool_dump_at_shutdown = 1 --在关闭 ...
- 问题:SpringBoot访问不到Controller
SpringBoot正常启动,其它配置都正常,以下是控制台打印: 解决方法: 将controller与application配置文件同层,是访问时无法扫描到相应的controller,故无法映射到相应 ...
- mysql 拾遗提高(函数、事务、索引)
目录 1.tips 2.事务(transaction) 3.索引(index) 4.数据库的导出和备份 5.函数 6.防SQL注入 7.使用Explain分析SQL语句 8.视图(view) 1.ti ...