题目链接: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]营业额统计的更多相关文章

  1. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  2. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

  3. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  4. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

  5. BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap

    1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...

  6. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  7. 洛谷P2234 [HNOI2002] 营业额统计 [splay]

    题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  8. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  9. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

随机推荐

  1. A - Network of Schools - poj 1236(求连通分量)

    题意:学校有一些单向网络,现在需要传一些文件,1,求最少需要向几个学校分发文件才能让每个学校都收到,2,需要添加几条网络才能在任意一个学校分发都可以传遍所有学校. 分析:首先应该求出来连通分量,进行缩 ...

  2. Struts2属性驱动与模型驱动

    为什么要使用属性驱动和模型驱动 struts2与struts很大的不同点在于,struts的execute方法提供了HttpServletRequest和HttpServletResponse方法在获 ...

  3. sql server 2008 在安装了活动目录以后无法启动服务了

    软件环境: windows server 2008 r2 ms sql server 2008 r2 在安装活动目录以前,数据库是正常运行的. 安装了活动目录以后,数据库启动时就提示无法启动.出错的信 ...

  4. swift菜鸟入门视频教程-04-集合类型

    本人自己录制的swift菜鸟入门,欢迎大家拍砖,有什么问题能够在这里留言. 主要内容: 数组(Arrays) 字典(Dictionaries) 集合的可变性(Mutability of Collect ...

  5. Spark的日志配置

    在測试spark计算时.将作业提交到yarn(模式–master yarn-cluster)上,想查看print到控制台这是imposible的.由于作业是提交到yarn的集群上,so 去yarn集群 ...

  6. EA+svn实现UML的版本号控制

    一.安装软件 1.VisualSvn Server svnserver 2.Tortoise Svn svnclient 3.Slik-Subversion-1.7.8-x64版本号控制插件 4.En ...

  7. [ES6] Objects create-shorthand && Destructuring

    Creating Object: Example 1: let name = "Brook"; let totalReplies = 249; let avatar = " ...

  8. Qt 格式化字符串

    Qt字符串格式化性能比较 Qt字符串格式方法有三种, QString::arg(), QString::sprinft()和QStringList::join().今天我做了个简单的性能测试, 希望对 ...

  9. 打印HTML页面部分区域javascript代码

    function preview(oper) { if (oper < 10) { bdhtml = window.document.body.innerHTML; //获取当前页的html代码 ...

  10. String.PadLeft - 格式对齐

    语法:public string PadLeft( int totalWidth, char paddingChar ) 概述,实现的效果是右对齐,返回一个总长度为 totalWidth,如果实际长度 ...