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天的最小波动值 ...
随机推荐
- KVM virsh常用命令篇
1.查看运行的虚拟机 virsh list 2.查看所有的虚拟机(关闭和运行的虚拟机) virsh list --all 3.连接虚拟机 virsh console +域名(虚拟机的名称) 4.退出虚 ...
- java基础43 IO流技术(输入字节流/缓冲输入字节流)
通过File对象可以读取文件或者文件夹的属性数据,如果要读取文件的内容数据,那么我们就要使用IO技术. 一.输入字节流 输入字节流的体系: -------| InputStream:所有输入字节流的 ...
- Java OOM学习
转载自原文: 什么是java OOM?如何分析及解决oom问题? 什么是OOM? OOM,全称"Out Of Memory",翻译成中文就是"内存用完了",表现 ...
- VFS,super_block,inode,dentry—结构体图解
总结: VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销. VFS的作用就是屏蔽各类文件系统的差异,给用户.应用程序.甚至Linux其他管理模块提供统一的接口集合. 管理VFS数据结构的组成 ...
- nginx + tomcat 集群记录
昨天晚写的时候已经下班了.时间紧迫.写的略简! 昨天说过.接到部署的任务之后.首先想到的是apache httpserver 毕竟pache.我们接触的比较多!然而部署之后遇到了很多问题.比如apac ...
- **PHP删除数组中特定元素的两种方法array_splice()和unset()
方法一: 复制代码代码如下: <?php$arr1 = array(1,3, 5,7,8);$key = array_search(3, $arr1); if ($key !== false) ...
- centos下安装zabbix
1. 安装mysql CREATE DATABASE zabbix;GRANT ALL ON zabbix.* TO 'zabbix'@'192.168.19.%' IDENTIFIED BY '12 ...
- 自己封装的php Curl并发处理,欢迎提出问题优化。
因为项目需要,发现一个一个发送请求实在太慢,无奈之下,我们可以封装一个并发处理的curl请求批处理句柄来减少重复创建句柄的问题 代码如下: /* *@param array $data url的参数 ...
- spring boot get和post请求,以及requestbody为json串时候的处理
GET.POST方式提时, 根据request header Content-Type的值来判断: application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的 ...
- ubuntu16.04编译安装mysql5.7
1.安装编译依赖 sudo apt-get install make cmake gcc g++ bison libncurses5-dev build-essential 2.下载mysql5.7源 ...