Splay入门题目 [HNOI2002]营业额统计
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588
这道题貌似很多中做法,我先是用multiset交了一发,然后又写了一发splay。
multiset做法,这个其实就是二分了,只是用set来保持加入一个元素时保持有序
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
multiset<int>S;
multiset<int>::iterator it;
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
while (~scanf ("%d",&n))
{
int ans = ;
S.clear();
for (int i = ; i < n; i++)
{
int x;
if (scanf ("%d",&x) == EOF)
x = ;
it = S.lower_bound(x);
if (it == S.begin())
ans += abs(*it-x);
else
{
int s1 = *it;
int s2 = *--it;
ans += min(abs(s1 - x),abs(s2 - x));
}
S.insert(x);
}
printf("%d\n",ans);
}
return ;
}
splay
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF)
return ;
while(c!='-'&&(c<''||c>''))
c=getchar();
sgn = (c=='-')?-:;
ret =(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='')
ret=ret*+(c-'');
ret*=sgn;
return ;
}
const int maxn = 1e5+;
int fa[maxn],son[maxn][],key[maxn],tot,root; void addNode(int &r,int father,int k)
{
r = ++tot;
fa[r] = father;
key[r] = k;
son[r][] = son[r][] = ;
} void Rotate(int r,int kind)
{
int y = fa[r];
son[y][!kind] = son[r][kind];
fa[son[r][kind]] = y;
if (fa[y])
son[fa[y]][son[fa[y]][]==y] = r;
son[r][kind] = y;
fa[r] = fa[y];
fa[y] = r;
}
void splay(int r,int goal)
{
while (fa[r] != goal)
{
if (fa[fa[r]] == goal)
{
Rotate(r,son[fa[r]][] == r);
}
else
{
int y = fa[r];
int kind = (son[fa[y]][] == y);
if (son[y][kind] == r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
if (goal == )
root = r;
}
bool Insert(int k)
{
int r = root;
while (son[r][k > key[r]])
{
if (k == key[r])
{
splay(r,);
return false;
}
r = son[r][k>key[r]];
}
addNode(son[r][k>key[r]],r,k);
splay(son[r][k>key[r]],);
return true;
}
int get_pre(int r)
{
int tmp = son[r][];
if (tmp == )
return inf;
while (son[tmp][])
tmp = son[tmp][];
return key[tmp];
}
int get_next(int r)
{
int tmp = son[r][];
if (tmp==)
return inf;
while (son[tmp][])
tmp = son[tmp][];
return key[tmp] ;
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n;
while (~scanf ("%d",&n))
{
tot = ;
root = ;
int ans = ;
for (int i = ; i < n; i++)
{
int x;
if (scanf ("%d",&x) == EOF)
x = ;
if (i == )
{
ans += x;
addNode(root,,x);
}
else
{
if (!Insert(x))
continue;
int l = get_pre(root);
int r = get_next(root);
ans += min(abs(x-l),abs(x-r));
//ans += min(l,r);
}
}
printf("%d\n",ans);
}
return ;
}
Splay入门题目 [HNOI2002]营业额统计的更多相关文章
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
随机推荐
- MYSQL存储过程,清除指前缀的定表名的数据
MYSQL存储过程,清除指前缀的定表名的数据 DELIMITER $$ DROP PROCEDURE IF EXISTS `drop_table`$$ ),)) BEGIN ) DEFAULT NUL ...
- [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子
转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...
- Linux 与 BSD 有什么不同?
Linux 与 BSD 有什么不同? 这篇文章是别人写的,并做了一点修改. 汉澳sinox就是基于bsd开发的,因此能够理解为一个bsd分支,可是由于sinox不开源,被排除在外.bsd不是商业软件, ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- [ACM] POJ 1442 Black Box (堆,优先队列)
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7099 Accepted: 2888 Descrip ...
- AIX-du
du命令显示用于文件的块的数量.如果指定的File参数实际上是一个目录,就要报告该目录内的所有文件.如果没有提供 File参数,du命令使用当前目录内的文件.如果File参数是一个目录,那么报告的块的 ...
- [Python爬虫笔记][随意找个博客入门(一)]
[Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...
- HttpClient get返回String类型 JAVA
public static String httpGet(String url) { // get请求返回结果 String strResult = ""; try { Defau ...
- 服务器上开启远程sqlserver小细节
之前按网络上过程配置完成后,连接都很正常.今天看的时候,突然就连接不上了.弄了半天,终于知道问题在哪里了.
- [c#]asp.net开发微信公众平台(2)多层架构框架搭建和入口实现
上篇已经设计出比较完善的数据库了,这篇开始进入代码. 首先把上篇设计的数据库脚本在数据库中执行下,生成数据库,然后在VS中建立项目,为了方便理解和查看,我设计的都是很直白的类名和文件名,没有命名空间 ...