标签:des style color os 数据 io for 代码
最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。
每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。
1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b那么领养者将会领养特点值为a-b的那只宠物。
2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。
一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。
【任务描述】
你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。
第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
5
0 2
0 4
1 3
1 2
1 5
3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
这题尼玛敲了一个上午,再加个午睡的时候敲出来的T了,而且T得很离谱,居然10010ms,我靠,瞬间不知道怎么改了。
算了,先做比较简单的伸展树吧,以后再回来看这题,把代码留着。哪位大神路过也可以帮我看看,哪里T了,不胜感激!!!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<bitset>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1000000007
#define maxn 10010
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int pre[maxn];//前驱
int key[maxn];//键值
int ch[maxn][2];//左右孩子,0为左孩子,1为右孩子
int root,tot,total;//根节点,节点数量
void reset()
{
tot=root=total=0;
mem(ch,0);
mem(pre,0);
mem(key,0);
}
void Treaval(int x)
{
if(x)
{
Treaval(ch[x][0]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d ,val = %2d \n",x,ch[x][0],ch[x][1],pre[x],key[x]);
Treaval(ch[x][1]);
}
}
void debug()
{
printf("%d\n",root);
Treaval(root);
}
void newnode(int &r,int father,int k)
{//在r位置,父亲为father,新建一个值点。
r=++tot;total++;
pre[r]=father;
key[r]=k;
ch[r][0]=ch[r][1]=0;
}
void rotate(int x,int kind)//kind=1表示右旋,kind=0表示左旋
{
int y=pre[x];
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
ch[x][kind]=y;
if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
pre[y]=x;
}
void splay(int x,int goal) //把x伸展到目标位置
{
while(pre[x]!=goal)
{
if(pre[pre[x]]==goal) rotate(x,ch[pre[x]][0]==x);
else
{
int y=pre[x];
int kind=ch[pre[y]][0]==y;
if(ch[y][kind]==x) rotate(x,!kind),rotate(x,kind);
else rotate(y,kind),rotate(x,kind);
}
}
if(!goal) root=x;//把root更新成x
}
int insert(int &x)//插入值x
{
int r=root;
while(ch[r][key[r]<x])
{
if(key[r]==x) {splay(r,0);return r;}
r=ch[r][key[r]<x];
}
newnode(ch[r][x>key[r]],r,x);
x=ch[r][x>key[r]];//传回新结点的编号
splay(x,0);
return x;//返回插入结点的编号
}
int find(int x)//查找键值为x的结点编号
{
int r=root;
if(!r) return -INF;//树未建,未找到
if(key[r]==x) return r;
while(ch[r][x>key[r]])
{
r=ch[r][x>key[r]];
if(key[r]==x) return r;
}
return -INF;//未找到
}
int getmax(int x)//得到这个子树的最大值结点编号
{
while(ch[x][1]) x=ch[x][1];
return x;
}
int getmin(int x)//得到这个子树的最小值结点编号
{
while(ch[x][0]) x=ch[x][0];
return x;
}
void Delete(int &x)//删除节点x
{
if(!ch[x][0]&&!ch[x][1])//若此结点是叶结点
{
if(x==root) {reset();return ;}
int y=pre[x];
if(ch[y][0]==x) ch[y][0]=0;
else ch[y][1]=0;
pre[x]=0;
}
else if(ch[x][0]&&!ch[x][1])
{
int m=getmax(ch[x][0]);
if(x==root) root=m;
splay(m,x);
pre[m]=pre[x];
}
else if(!ch[x][0]&&ch[x][1])
{
int m=getmin(ch[x][1]);
if(x==root) root=m;
splay(m,x);
pre[m]=pre[x];
}
else if(ch[x][0]&&ch[x][1])
{
int m=getmax(ch[x][0]);
splay(m,x);
ch[m][1]=ch[x][1];
pre[ch[x][1]]=m;
if(x==root) root=m;
}
}
int get_pre(int &x)//寻找节点x的前驱的值,未找到则返回INF
{
x=ch[x][0];
if(!x) return -INF;
while(ch[x][1]) x=ch[x][1];
return key[x];
}
int get_next(int &x)
{
x=ch[x][1];
if(!x) return INF;
while(ch[x][0]) x=ch[x][0];
return key[x];
}
int main()
{
int sum=0,a,b,n;
cin>>n;
reset(),reset();
for(int i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
if(!a)//根据题目第一第二点的要求知道宠物和人的操作一样
{
if(total<1&&total>0)
{
int bb=b;
int r=insert(bb);
int r1=r,r2=r;
int x=get_pre(r1);
int y=get_next(r2);
if(b-x<=y-b)
{
sum=(sum+b-x)%1000000;
Delete(r1),Delete(r);
}
else
{
sum=(sum+y-b)%1000000;
Delete(r2),Delete(r);
}
total-=2;
}
else if(!tot&&!tot)
newnode(root,0,b);
else insert(b);
}
else
{
if(total>0)
{
int bb=b;
int r=insert(bb);
int r1=r,r2=r;
int x=get_pre(r1);
int y=get_next(r2);
//dog.debug();
if(b-x<=y-b)
{
sum=(sum+b-x)%1000000;
//cout<<sum<<' '<<x<<' '<<y<<endl;
Delete(r1),Delete(r);
}
else
{
sum=(sum+y-b)%1000000;
//cout<<sum<<' '<<x<<' '<<y<<endl;
Delete(r2),Delete(r);
}//dog.debug();
total-=2;
}
else
{
if(tot) insert(b);
else newnode(root,0,b);
}
}
}
cout<<sum<<endl;
return 0;
}
wikioi 1285 伸展树delete操作,布布扣,bubuko.com
标签:des style color os 数据 io for 代码
原文地址:http://blog.csdn.net/u011466175/article/details/38143883