[HNOI2002]营业额统计 Splay tree
Splay tree入门题,学好代码风格,学习HH大牛的,传送门。。
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int pre[N],key[N],ch[N][],root,tot; //分别表示父结点,键值,左右孩子(0为左孩子,1为右孩子),根结点,结点数量
int n;
//新建一个结点
void addn(int &r,int fa,int k)
{
r=++tot;
pre[r]=fa;
key[r]=k;
ch[r][]=ch[r][]=; //左右孩子为空
}
//旋转,kind为1为右旋,kind为0为左旋
int Rotate(int x,int kind)
{
int y=pre[x],z=pre[y];
//类似SBT,要把其中一个分支先给父节点
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
//如果父节点不是根结点,则要和父节点的父节点连接起来
if(z)ch[z][ch[z][]==y]=x;
pre[x]=z;
ch[x][kind]=y;
pre[y]=x;
}
//Splay调整,将根为r的子树调整为goal
int Splay(int r,int goal)
{
int y,kind;
while(pre[r]!=goal){
//父节点即是目标位置,goal为0表示,父节点就是根结点
y=pre[r];
if(pre[y]==goal){
Rotate(r,ch[y][]==r);
}
else {
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;
} int Insert(int k)
{
int r=root;
while(ch[r][k>key[r]]){
//不重复插入
if(key[r]==k){
Splay(r,);
return ;
}
r=ch[r][k>key[r]];
}
addn(ch[r][k>key[r]],r,k);
//将新插入的结点更新至根结点
Splay(ch[r][k>key[r]],);
return ;
}
//找前驱,即左子树的最右结点
int getpre(int x)
{
if(!ch[x][])return -INF;
x=ch[x][];
while(ch[x][])x=ch[x][];
return key[x];
}
//找后继,即右子树的最左结点
int getsuf(int x)
{
if(!ch[x][])return INF;
x=ch[x][];
while(ch[x][])x=ch[x][];
return key[x];
} int main()
{
// freopen("in.txt","r",stdin);
int i,a,ans;
while(~scanf("%d",&n))
{
ans=root=tot=;
for(i=;i<n;i++){
if(scanf("%d",&a)==EOF)a=;
if(i==){
ans+=a;
addn(root,,a);
}
else {
if(Insert(a)==)continue;
ans+=Min(a-getpre(root),getsuf(root)-a);
}
}
printf("%d\n",ans);
}
return ;
}
[HNOI2002]营业额统计 Splay tree的更多相关文章
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- 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天的最小波动值 ...
- NOIP 营业额统计 splay tree 纯模板
2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 389 ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- HNOI2002 营业额统计 [Splay]
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
随机推荐
- PAT甲级1016. Phone Bills
PAT甲级1016. Phone Bills 题意: 长途电话公司按以下规定向客户收取费用: 长途电话费用每分钟一定数量,具体取决于通话时间.当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也 ...
- 函数调用过程中,函数参数的入栈顺序,why?
C语言函数参数入栈顺序为从右至左.具体原因为:C方式参数入栈顺序(从右至左)的好处就是可以动态变化参数个数.通过栈堆分析可知,自左向右的入栈方式,最前面的参数被压在栈底.除非知道参数个数,否则是无法通 ...
- Windows UWP开发系列 – 3D变换
在Win8.1中,引入了一个PlaneProjection可以实现3D变换,但它的变换方式比较简单,只能实现基本的旋转操作.在Windows 10 UWP中,引入了一个更加强大的3D变换Transfo ...
- OSChina.net 的 Tomcat 配置 server.xml 参考
这是目前 oschina.net 正在使用的 tomcat 的 server.xml 的配置文件内容 <Server port="9005" shutdown="S ...
- ArcGIS教程:编辑特征
摘要 通过合并.又一次编号和删除类特征来编辑和更新特征文件. 使用方法 · 编辑特征工具同意您通过下面全部操作或某一操作来改动现有特征文件: 合并一组特征类 又一次编号特征类 ID 删除不须要的特征 ...
- Shell升级,/bin/bash版本号4.1到4.3
bash环境变量存在随意代码运行漏洞:"通过CGI请求方式能够导致远程代码运行,进而导致server被入侵.危害严重.且官方发布补丁也被绕过", [漏洞影响]: 1)bash受影响 ...
- LINUX开启允许对外访问的网络端口命令
LINUX通过下面的命令可以开启允许对外访问的网络端口: /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT #开启8000端口 /etc/rc ...
- google支付回调验证
原文链接: https://my.oschina.net/lemonzone2010/blog/398736 Google支付问题 20150218,挂机的日本服务器出现google支付被刷单现象,虽 ...
- 《CUDA并行程序设计:GPU编程指南》
<CUDA并行程序设计:GPU编程指南> 基本信息 原书名:CUDA Programming:A Developer’s Guide to Parallel Computing with ...
- Trie树统计单词前缀
输入 输入的第一行为一个正整数n.表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦).单词由不超过10个的小写英文字母组成,可能存在同样的单词.此时应将其视作不同的单 ...