[USACO17DEC]Milk Measurement(平衡树)
题意
最初,农夫约翰的每头奶牛每天生产G加仑的牛奶 (1≤G≤109)(1≤G≤10^9)(1≤G≤109) 。由于随着时间的推移,奶牛的产奶量可能会发生变化,农夫约翰决定定期对奶牛的产奶量进行测量,并将其记录在日志中。
他的日志中的记录如下:
35 1234 -2
14 2345 +3
第一个条目表明:在第35天,1234号奶牛的产奶量比上次测量时降低了2加仑。
第二个条目表明:在第14天,2345号奶牛的产奶量比上次测量时增加了3加仑。
农夫约翰只有在任何一天内做最多一次测量的时间(即每天最多做一次测量,但可能不做)。不幸的是,约翰有点杂乱无章,他不一定按照时间顺序记下测量结果。为了保持奶牛的产奶动力,农夫约翰自豪地在谷仓的墙上展示了目前产奶量最高的奶牛的照片(如果有若干头奶牛的产奶量最高,他就会展示所有的图片)。
请求出约翰需要调整所展示的照片的次数。
请注意,农夫约翰有一大群奶牛。所以尽管日志中记录了一些奶牛改变了产奶量,但仍然还有很多奶牛的产奶量保持在G加仑。
题解
每一个时间只有一个奶牛产奶量会变化。
我们算出变化前的产奶量的排名,和变化后的产奶量排名。
如果发现是从第一变到不是第一,或从不是第一到第一,那照片一定发生变化。
如果变化后,和变化前都是第一。照片可能变也可能不会变,分四种情况讨论:
1,、可能一开始有很多奶牛并列第一,然后其中的一个奶牛独占了第一。这时照片会变。
2、一开始一个奶牛是第一,然后产奶量增加还是第一,显然不变。
3、也有可能开始一个奶牛第一然后,产奶量下降,变得和第二一样,这时会变。
4、一开始一个奶牛是第一,然后产奶量下降但比第二高还是第一,显然不变。
所以我们找出产奶量为变化之前的奶牛的数量,和产奶量为变化之后的奶牛的数量,判断相不相等即可。
这些东西都可以用平衡树维护。(权值线段树,主席树什么的当然也可以)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
const int N=;
map<int,int>ma;
int n,g,mx,num;
int a[N],b[N],ans[N];
int cnt[N],size[N],ch[N][],fa[N],v[N];
int root,tot;
struct hhh{
int t,id,w;
}c[N];
bool cmp(hhh a,hhh b){
return a.t<b.t;
}
void update(int x){
size[x]=size[ch[x][]]+size[ch[x][]]+cnt[x];
}
int son(int x){
return x==ch[fa[x]][];
}
void rotate(int x){
int y=fa[x],z=fa[y],a=son(x),b=son(y),c=ch[x][!a];
if(z)ch[z][b]=x;
else root=x;
fa[x]=z;
if(c)fa[c]=y;
ch[x][!a]=y;
ch[y][a]=c;
fa[y]=x;
update(y);
update(x);
}
void splay(int x,int f){
while(fa[x]!=f){
int y=fa[x];
int z=fa[y];
if(z==f)rotate(x);
else{
if(son(y)==son(x))rotate(y);
else rotate(x);
rotate(x);
}
}
if(f==)root=x;
}
void ins(int x){
int p=root;
int f=;
while(p&&v[p]!=x){
f=p;
size[p]++;
p=ch[p][v[p]<x];
}
if(p){
size[p]++;
cnt[p]++;
}
else{
p=++tot;
if(f)ch[f][v[f]<x]=p;
size[p]=cnt[p]=;
v[p]=x;
fa[p]=f;
}
splay(p,);
}
int getmn(int rt){
int p=rt,ans=-;
while(p){
ans=p;
p=ch[p][];
}
return ans;
} void del(int rt,int x){
if(v[rt]==x){
if(cnt[rt]>)cnt[rt]--,size[rt]--;
else{
splay(rt,);
int p=getmn(ch[rt][]);
if(p!=-){
splay(p,rt);
root=p;
fa[p]=;
ch[p][]=ch[rt][];
fa[ch[rt][]]=p;
update(p);
}
else {
root=ch[rt][];
fa[root]=;
}
}
return;
}
if(x<v[rt])del(ch[rt][],x),update(rt);
else del(ch[rt][],x),update(rt);
}
int rank(int rt,int k){
if(v[rt]==k){
splay(rt,);
return size[ch[rt][]]+;
}
if(k<v[rt])return rank(ch[rt][],k);
else return rank(ch[rt][],k);
}
int getsame(int rt,int x){
if(v[rt]==x){
splay(rt,);
return cnt[rt];
}
if(x<v[rt])return rank(ch[rt][],x);
else return rank(ch[rt][],x);
}
int main(){
scanf("%d%d",&n,&g);
for(int i=;i<=n;i++){
scanf("%d%d",&c[i].t,&c[i].id);
char ch;
cin>>ch;
scanf("%d",&c[i].w);
if(ch=='-')c[i].w=-c[i].w;
b[i]=c[i].id;
}
sort(b+,b++n);
int cnt=unique(b+,b++n)-b-;
for(int i=;i<=n;i++){
c[i].id=lower_bound(b+,b++cnt,c[i].id)-b;
}
sort(c+,c++n,cmp);
for(int i=;i<=cnt+;i++){
a[i]=g;
ins(g);
}
ma[g]=cnt+;
for(int i=;i<=n;i++){
// cout<<";asjhfljashfjashdfasdf"<<endl;
if(c[i].w==)continue;
int k=rank(root,a[c[i].id]);
int num1=getsame(root,a[c[i].id]);
// cout<<k<<" "<<num1<<endl<<"aaa"<<endl;
del(root,a[c[i].id]);
a[c[i].id]+=c[i].w;
ins(a[c[i].id]);
int kk=rank(root,a[c[i].id]);
int num2=getsame(root,a[c[i].id]);
if((kk==&&k!=)||(k==&&kk!=)||(num1!=num2&&kk==&&k==)){
ans[++num]=c[i].t;
}
}
printf("%d\n",num);
// for(int i=1;i<=num;i++){
// printf("%d\n",ans[i]);
// }
return ;
}
/*
4 10
7 3 +3
4 2 -1
9 3 -1
1 1 +2
*/
[USACO17DEC]Milk Measurement(平衡树)的更多相关文章
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
- [BZOJ3223]Tyvj 1729 文艺平衡树
[BZOJ3223]Tyvj 1729 文艺平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区 ...
- [BZOJ3224]Tyvj 1728 普通平衡树
[BZOJ3224]Tyvj 1728 普通平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个) ...
- Principles of measurement of sound intensity
Introduction In accordance with the definition of instantaneous sound intensity as the product of th ...
- BZOJ3223: Tyvj 1729 文艺平衡树 [splay]
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3595 Solved: 2029[Submit][Sta ...
- [普通平衡树treap]【学习笔记】
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9046 Solved: 3840[Submit][Sta ...
- BZOJ 3224: Tyvj 1728 普通平衡树
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 9629 Solved: 4091[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- 机器学习中的相似性度量(Similarity Measurement)
机器学习中的相似性度量(Similarity Measurement) 在做分类时常常需要估算不同样本之间的相似性度量(Similarity Measurement),这时通常采用的方法就是计算样本间 ...
随机推荐
- js获取当前位置的地理坐标(经纬度)
在 freecodecamp 上学习时,碰到获取地理坐标问题.写个笔记纪录下. if(navigator.geolocation) { navigator.geolocation.getCurrent ...
- div+css布局自适应小结
一.两栏布局(左定宽,右自动)1. float + margin即固定宽度元素设置float属性为left,自适应元素设置margin属性,margin-left应>=定宽元素宽度.举例: &l ...
- 21_HTML&CSS
今日内容: 1. HTML标签:表单标签2. CSS: HTML标签:表单标签 * 表单: * 概念:用于采集用户输入的数据的.用于和服务器进行交互. * form:用于定义表单的.可以定义一个范围 ...
- 动态生成的dom元素绑定事件
要求:要绑定到父元素上$(".school_Inlists").on("click",".chose_Inbtn",function(){ ...
- 怎样验证layer.prompt输入的值为数值型???
JS中使用isNaN()判断layer.prompt输入的值为数值型,代码如下: layer.prompt({ title: '设置比值', }, function(value, index, ele ...
- tensorflow之tf.slice()
转载:https://www.jianshu.com/p/71e6ef6c121b https://www.cnblogs.com/chamie/p/11073363.html def slice(i ...
- 如何让Jboss的debug在myeclise上运行
1.在windows下运行jboss的debug.bat 看见监听的端口 2.打开myeclipse 点击选择 ①你要配置的名字(随意) ②myeclipse中选中该项目 ③jboss的启动的ip地址 ...
- Python 爬虫练习: 爬取百度贴吧中的图片
背景:最近开始看一些Python爬虫相关的知识,就在网上找了一些简单已与练习的一些爬虫脚本 实现功能:1,读取用户想要爬取的贴吧 2,读取用户先要爬取某个贴吧的页数范围 3,爬取每个贴吧中用户输入的页 ...
- NYIST 1030 Yougth's Game[Ⅲ]
Yougth's Game[Ⅲ]时间限制:3000 ms | 内存限制:65535 KB难度:4 描述有一个长度为n的整数序列,A和B轮流取数,A先取,每次可以从左端或者右端取一个数,所有数都被取完时 ...
- 推断CPU 是小端存储(Little endian)还是大端存储(Big endian)模式
第一个版本号: //return true in big-endian machines bool check_big_endian1() { int a = 0; int *p = &a; ...