Description

Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepared)。推销商品要遵守以下规则:

1. 想参与促销的顾客在自己的帐单上写下个人信息,然后将票据投入一个特制的投票箱中。

2. 促销期间,每天结束后,有2张票据将被取出——消费金额最大的和最小的两张帐单。消费金额最大的那位顾客得到的奖品价值等于取出的2张帐单的差额。

3. 为了避免多次得奖,所有取出的帐单将不再放回箱中,其余的票继续参加促销活动.

由于商场的顾客特别多,所以每天投票箱中都至少有2张帐单。你的任务是计算在促销期间,商家一共要送出多少前的礼品。

Code

#include <cstdio>
#include <algorithm>
#define N 1000010
#define lc(x) T[(x)][0]
using namespace std; int T[N][2],fa[N],k[N],tot,rt,Ans; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} void rotate(int p){
int q=fa[p],y=fa[q],x=(T[q][1]==p);
T[q][x]=T[p][x^1];fa[T[q][x]]=q;
T[p][x^1]=q;fa[q]=p;
fa[p]=y;
if(y){
if(T[y][0]==q) T[y][0]=p;
else if(T[y][1]==q) T[y][1]=p;
}
} void splay(int x){
for(int y;y=fa[x];rotate(x))
if(fa[y]) rotate((x==lc(y))==(y==lc(fa[y]))?y:x);
rt=x;
} void Insert(int x,int v){
if(!rt){
rt=++tot;
T[rt][0]=T[rt][1]=0;
fa[rt]=0;
k[rt]=v;
return;
}
int y;
for(;;){
y=T[x][k[x]<v];
if(!y){
y=++tot;
k[y]=v;
T[y][0]=T[y][1]=0;
fa[y]=x;
T[x][k[x]<v]=y;
break;
}
x=y;
}
splay(y);
} void Del(int x){
splay(x);
if(T[x][0]*T[x][1]==0) rt=T[x][0]+T[x][1];
else{
int t=T[x][1];
while(T[t][0]) t=T[t][0];
T[t][0]=T[x][0],fa[T[t][0]]=t;
rt=T[x][1];
}
fa[rt]=0;
} int f(int x,int b){
while(T[x][b]) x=T[x][b];
int r=k[x];
Del(x);
return r;
} int main(){
int day=read();
while(day--){
int n=read();
while(n--) Insert(rt,read());
Ans+=f(rt,1)-f(rt,0);
}
printf("%d\n",Ans);
return 0;
}

最新文章

  1. Python 监控nginx服务是否正常
  2. C/C++学习笔记----指针的理解
  3. asp.net MVC ajax上传文件
  4. 每天一道LeetCode--374. Guess Number Higher or Lower
  5. 2013 Multi-University Training Contest 1 3-idiots
  6. 10行Python代码解决约瑟夫环(模拟)
  7. Spring - Spring容器概念及其初始化过程
  8. 有关promise的技巧
  9. intellij idea创建maven项目
  10. 2017-12-14python全栈9期第一天第六节之用户交互
  11. lf-8.4 数据的增删改
  12. jdgui反编译+javac编译=无源文件改动代码
  13. vue-cli+webpack在生成的项目中使用bootstrap方法(一)
  14. AndroidA——背景选择器selector用法汇总(一)
  15. 字符串中包含汉字和\u,显示出汉字来
  16. Web自动化测试框架Watir(基于Ruby) - 第2章 使用Watir写自动化测试脚本
  17. cakephp获取最后一条sql语句
  18. [二叉查找树] 1115. Counting Nodes in a BST (30)
  19. C# Directory和DirectoryInfo类(文件目录操作)
  20. 关于c++中public &amp; private方法调用问题

热门文章

  1. 在MFC对话框中快速集成三维控件
  2. 记一次有关GET/POST请求的Debug经历
  3. 表格&lt;table&gt;
  4. Centos_linux系统的区别及实际查看
  5. javascript HTML静态页面传值的四种方法
  6. hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
  7. 谈谈bootstrap在实践中的应用
  8. linux 命令——41 ps(转)
  9. js 正则匹配(去掉html标签)
  10. hdu-2255 奔小康赚大钱---KM模板