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天的最小波动值 ...
随机推荐
- Scala中“=>”用法及含义
=> has several meanings in Scala, all related to its mathematical meaning as implication. 1. In a ...
- DOS命令基础,包涵DOS库说明书
20种常用的DOS命令小结 作者: 字体:[增加 减小] 类型:转载 DOS命令总共大约有一百个(包括文本编辑.查杀病毒.配置文件.批处理等),我们这里详细介绍二十个常用的DOS命令 先介 ...
- [USACO16OPEN]262144
传送门啦 其实大家可以先看一下这个题 [USACO16OPEN]248 分析: 数据范围很奇特:n特别,a[i]特别——如果O(N^3)能接受就直接区间DP水过了,但是不行,于是考虑设计一个状态囊括a ...
- Linux基础 - crontab
列出当前用户设置的定时任务 crontab -l 编辑定时任务 crontab -e 用法 m h dom mon dow * * * * * command 字段详解: *:any m: minut ...
- 四B象限图
- qlserver排序规则在全角与半角处理中的应用
--1.查询区分全角与半角字符--测试数据DECLARE @t TABLE(col varchar(10))INSERT @t SELECT 'aa'UNION ALL SELECT 'Aa'UNIO ...
- Python学习笔记:出生日期转化为年龄
在数据挖掘项目中,有时候个体的出生日期包含信息量过大,不适合作为一个有效数据进入模型算法训练,因此有必要把出生日期转化为年龄age,age是一个很好的特征工程指示变量. import pandas a ...
- Kaldi 安装
以后要重点搞caldi了,虽然集群上有,但还是本地安装一下吧. 参考 Kaldi 学习手记(一):Kaldi 的编译安装 在 ubuntu 下安装 kaldi 基本步骤 两个文章基本差不多 1 ...
- Deepin Linux安装MySQL方法
sudo apt-get install mysql-server apt-get install mysql-client sudo apt-get install libmysqlclient-d ...
- pct_free
SQL> select table_name,pct_free,pct_used from user_tables; TABLE_NAME PCT_FREE PCT_USED---------- ...