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最近被公司升任为营业部经理,他上 ...
随机推荐
- 第一个ServiceStack程序
1. https://github.com/ServiceStack/ServiceStack/wiki/Create-your-first-webservice 2. http://tech.pro ...
- 安装nodejs 后运行 npm 命令无响应处理方法
安装和卸载过nodejs, 也编辑过 C:\Users\{账户}\下的.npmrc文件. 再全新安装nodejs ,运行npm 命令,无响应. 处理方法,删除C:\Users\{账户}\下的.npmr ...
- LOADRUNNER8.1卸载
卸载LOADRUNNER8.1后,不能正常又一次安装的问题. Loadrunner 8.1 安装1.下载Loadrunner8.1 (官方英文版) 2.安装Loadrunner8.1 3.破解:htt ...
- Python3.2官方文档翻译--继承
6.5 继承 当然,一门语言特性假设不支持继承那么名称类就失去了价值.子类继承父类的方法例如以下: class DerivedClassName(BaseClassName): <stateme ...
- 【iOS开发之C语言】sprintf,strncpy,strcmp三个函数的区别
strncpy 这个函数用于将源字符串的内容拷贝到目标字符串,会覆盖掉目标字符串的之前内容 ] = "love"; char str2[] = "cool"; ...
- (转)Phonegap VS AppCan
简介 Phonegap PhoneGap是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的快速开发平台.它使开发者能够利用iPhone,Android,Palm,Sym ...
- windows身份验证,那么sqlserver的连接字符串的
Data Source=计算机名称或ip地址;Initial Catalog=数据库名称;Integrated Security=True windows身份验证不需要psw的Provider=SQL ...
- uva 10222 - Decode the Mad man
#include <iostream> #include <string> #include <cctype> using namespace std; int m ...
- JS将毫秒转换成时间格式
JavaScript Date(日期)对象 实例 getTime():返回从 1970 年 1 月 1 日至今的毫秒数. setFullYear(): 设置具体的日期. toUTCString():将 ...
- zip命令的用法
语法zip [参数] [打包后的文件名] [打包的目录路径] 参数列表-a 将文件转成ASCII模式-F 尝试修复损坏的压缩文件-h 显示帮助界面-m 将文件压缩之后,删除源文件-n 特定字符串,不压 ...