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天的最小波动值 ...
随机推荐
- javascript本地缓存方案-- 存储对象和设置过期时间
cz-storage 解决问题 1. 前端js使用localStorage的时候只能存字符串,不能存储对象 cz-storage 可以存储 object undefined number string ...
- No.5 selenium学习之路之多窗口句柄
多窗口相关操作 获取当前句柄 c_handle = driver.current_window_handle 获取所有句柄 all_handle = driver.window_handles 切换到 ...
- [java笔记]动态数组
private int count;//计数器 private int ary[] = new int [3]; if(count >= ary.length){ //数组动态扩展 int ne ...
- IntelliJ IDEA 自动导入包的问题
我们再使用IDE写代码的时候,往往需要 鼠标点中这个类 然后 使用 alt+enter ,导入响应的包,如果导入的包比较多,一个一个点 也是费事. 因为用手动,有可能需要你选择导入那个包,有时候类名 ...
- 涨姿势系列之——内核环境下花式获得CSRSS进程id
这个是翻别人的代码时看到的,所以叫涨姿势系列.作者写了一个获取CSRSS进程PID的函数,结果我看了好久才看懂是这么一个作用.先放上代码 HANDLE GetCsrPid() { HANDLE Pro ...
- Struts 2 - Environment Setup
Our first task is to get a minimal Struts 2 application running. This chapter will guide you on how ...
- Visual Studio Code 常用插件整理
常用插件说明: 一.HTML Snippets 超级使用且初级的H5代码片段以及提示 二.HTML CSS Support 让HTML标签上写class智能提示当前项目所支持的样式 三.Debugg ...
- Mongodb配置:error:10061 由于目标计算机积极拒绝,无法连接
相信很多学Node的同学,在进入MongoDB后台管理 Shell的时候都会“遇到error:10061 由于目标计算机积极拒绝,无法连接”这种情况,很多情况都是dbpath与dblog的路径没有配置 ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp
E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...
- File.separator与 \分隔符
在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常. 比如说要在temp目录下建立一个te ...