HNOI_2002 营业额统计(Splay)
此题可以用STL的multiset解决,也可以手打一棵伸展树(Splay)来求前驱与后驱。
使用multiset:
#include<iostream>
#include<set>
#include<algorithm>
#include<stdio.h>
using namespace std; typedef long long LL; multiset<LL> se; /*
LL abs(LL a)
{
return a > 0 ? a : -a;
}*/ int main()
{
int n;
scanf("%d", &n);
LL a, ans = 0;
multiset<LL>::iterator it, tmp1,tmp2;
for (int i = 0; i < n; i++)
{
if (scanf("%lld", &a) == EOF) a = 0;
se.insert(a);
if (i == 0)
{
ans = a;
continue;
}
it = se.find(a);
tmp1 = tmp2 = it;
if (it == se.begin())
{
it++;
ans += (*it - a);
continue;
}
tmp2++;
if (tmp2 == se.end())
{
it--;
ans += (a - *it);
continue;
}
tmp1--;
ans += min(a - *tmp1, *tmp2 - a); }
printf("%lld\n", ans);
return 0;
}
使用Splay的代码:
#include<iostream>
#include<algorithm>
#define leftSon ch[0]
#define rightSon ch[1]
using namespace std; struct node
{
node *father, *ch[2];
int key, sam;
node(){ sam = 0; }
}; node *root;
node R;
bool flag = 1; void Rotate(node *now, int c)
{
node *y = now->father;
y->ch[!c] = now->ch[c];
if (now->ch[c] != NULL)
now->ch[c]->father = y;
now->father = y->father;
if (y->father != NULL)
if (y->father->ch[0] == y)
y->father->ch[0] = now;
else y->father->ch[1] = now;
now->ch[c] = y, y->father = now;
if (y == root) root = now;
} void Splay(node *now, node *f) // Splay
{
for (; now->father != f;)
if (now->father->father == f)
if (now->father->ch[0] == now)
Rotate(now, 1);
else Rotate(now, 0);
else
{
node *y = now->father, *z = y->father;
if (z->ch[0] == y)
if (y->ch[0] == now)
Rotate(y, 1), Rotate(now, 1);
else
Rotate(now, 0), Rotate(now, 1);
else if (y->ch[1] == now)
Rotate(y, 0), Rotate(now, 0);
else
Rotate(now, 1), Rotate(now, 0);
}
} void ini()
{
root = &R;
root->father = root->ch[0] = root->ch[1] = NULL;
} void Insert(int x, node *now, node *pre)
{
if (now != NULL&&now->key == x){ now->sam++; return; }
if (now == root&&flag)
{
now->key = x;
now->rightSon = NULL;
now->leftSon = NULL;
now->father = pre;
now->sam++;
flag = 0;
return;
}
if (now == NULL)
{
node *newNode;
newNode = new node;
newNode->key = x;
newNode->rightSon = NULL;
newNode->leftSon = NULL;
newNode->father = pre;
newNode->sam = 1;
if (x > pre->key)
pre->rightSon = newNode;
else
pre->leftSon = newNode;
Splay(newNode, NULL);
return;
}
if (x > now->key)
Insert(x, now->rightSon, now);
else
Insert(x, now->leftSon, now);
} node* find(int x, node *now)
{
if (now == NULL)
return NULL;
if (now->key == x)
return now;
if (x < now->key)
return find(x, now->ch[0]);
else
return find(x, now->ch[1]);
return NULL;
} node* searchMin(node *now)
{
if (now == NULL)
return NULL;
if (now->leftSon == NULL)
return now;
searchMin(now->leftSon);
} node* searchMax(node *now)
{
if (now == NULL)
return NULL;
if (now->rightSon == NULL)
return now;
searchMax(now->rightSon);
} void Delete(int x)
{
node *now = find(x, root);
if (now == NULL)
return;
if (now->key == x)
{
if (now->leftSon == NULL&&now->rightSon == NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = NULL;
else
now->father->rightSon = NULL;
return;
}
if (now->leftSon == NULL&&now->rightSon != NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = now->rightSon;
else
now->father->rightSon = now->rightSon;
now->rightSon->father = now->father;
return;
}
if (now->leftSon != NULL&&now->rightSon == NULL)
{
if (now->father->leftSon == now)
now->father->leftSon = now->leftSon;
else
now->father->rightSon = now->leftSon;
now->leftSon->father = now->father;
return;
}
node *tmp = searchMin(now->rightSon);
now->key = tmp->key;
now->sam = tmp->sam;
if (tmp->father->leftSon == tmp)
tmp->father->leftSon = NULL;
else
tmp->father->rightSon = NULL;
return;
}
} void middleTravel(node *now)
{
if (now == NULL)
return;
middleTravel(now->leftSon);
for (int i = 0; i < (now->sam); i++)
cout << now->key << " ";
middleTravel(now->rightSon);
} int main()
{
ini();
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++)
{
int a;
if (!(cin >> a))a = 0;
if (i == 0)
{
ans = a;
Insert(a, root, NULL);
continue;
}
node *tmp = find(a, root);
if (tmp != NULL)
{
Insert(a, root, NULL);
continue;
}
Insert(a, root, NULL);
tmp = find(a, root);
node *pre = searchMax(tmp->ch[0]);
node *nex = searchMin(tmp->ch[1]);
if (pre == NULL&&nex != NULL)
ans += nex->key - a;
else if (pre != NULL&&nex == NULL)
ans += a - pre->key;
else if (pre != NULL&&nex != NULL)
ans += min(a - pre->key, nex->key - a);
}
cout << ans << endl;
return 0;
}
HNOI_2002 营业额统计(Splay)的更多相关文章
- NOI 2002 营业额统计 (splay or fhq treap)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- NOIP 营业额统计 splay tree 纯模板
2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 389 ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
- 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天的最小波动值 ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
随机推荐
- 浏览器window产生的缓存九种解决办法
浏览器缓存(Browser Caching)是浏览器端保存数据用于快速读取或避免重复资源请求的优化机制,有效的缓存使用可以避免重复的网络请求和浏览器快速地读取本地数据,整体上加速网页展示给用户.浏览器 ...
- ios软件设计中注意点
1.取消系统自带渲染效果 2.取消屏幕旋转 3.项目中搜索丢失文件
- Mac 录制视频,并转为GIF格式
内容中包含 base64string 图片造成字符过多,拒绝显示
- Ubuntu中安装配置 JDK与apache
一,前期准备: 1.下载apach网址:https://tomcat.apache.org/download-90.cgi 3.下载:jdk网址:http://www.oracle.com/techn ...
- django第六天(模板相关,过滤器和标记)
django第6天 DTL简介 django template language django模板语言 语法: filter{{}} tag{% %} 简单变量的使用 视图函数可以通过两种方式将变量船 ...
- Python9-反射-day27(大年初三)
复习 class 类名(父类,父类2): 静态属性 = '' #静态属性 类属性 def __init__(self): #初始化方法 self.name = 'alex' def func(self ...
- gnu printf可变参数宏
可变参数的宏 标准C只支持可变参数的函数,意味着函数的参数可以是不固定的 例如printf()函数的原型是int printf(const char *format [,argument]...) 而 ...
- shell中test的使用
#/secondin/secondfirstshecho “please enter two numseconder”read firstread secondif test $first -eq $ ...
- Lex与Yacc学习(四)之Lex规范
Lex规范的结构 lex程序由三部分组成:定义段.规则段和用户子例程序段 ...定义段... %% ...规则段... %% ...用户子例程序段... 这些部分由以两个百分号组成的行分隔开.尽管某一 ...
- cs229_part5
这部分主要补充一些cs229没涉及到,但是实际上非常重要,而且是实际中真正会用的一些算法,即集成学习. 集成学习 问题背景 既然我们已经知道了很多学习算法,这些算法最终会输出一个结果.能不能把这些结果 ...