hdu-4585-Shaolin 平衡二叉树

来源:互联网 发布:java compare返回值 编辑:程序博客网 时间:2024/05/19 13:59

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4585

就是维持顺序的问题

用的平衡二叉树,太弱  不会 map/set 自己套的模板 写的平衡二叉树

200+的代码  用map 30+的代码  想哭。。。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <cstring>#include <algorithm>#define INF 10000010using namespace std;typedef int Elemtype;typedef struct Balanced_Binary_Tree{    Elemtype e;    int bf;    int id;    struct Balanced_Binary_Tree *child[2];}*AVL;int min(int a,int b){    if(a>b)        return b;    else        return a;}void setbit(char *i,char val,char pos){    if(pos==1)        (*i)=(*i)|1;    else    {        if(val==1)    (*i)=(*i)|2;        else    (*i)=(*i)&1;    }}char getbit(char i,char pos){    return (i&pos)&&1;}AVL createnode(Elemtype e,int id){    AVL node=NULL;    node=(AVL)malloc(sizeof(struct Balanced_Binary_Tree));    node->e=e;    node->bf=0;    node->id=id;    node->child[0]=node->child[1]=NULL;    return node;}void setfactor(AVL f,int button){    char fir=button/10,sec=button%10;    AVL s=f->child[fir],ss=s->child[sec];    char choice=ss->bf;    int a=1,b=0;    if(button==0 && s->bf==0)    f->bf=1,s->bf=-1;    else if(button==11 && s->bf==0)    f->bf=-1,s->bf=1;    else if(button==0 || button==11)    {        f->bf=0;        s->bf=0;    }    else    {        if(button==1)        {            a^=b,b^=a,a^=b;        }        if(choice==-1)    f->bf=a,s->bf=b;        else if(choice==0)    f->bf=s->bf=0;        else    f->bf=-b,s->bf=-a;        ss->bf=0;    }}void conversion(AVL *T,char direction){    AVL f=*T,s=f->child[direction];    f->child[direction]=s->child[!direction];    s->child[!direction]=f;    *T=s;}void keepbalance(AVL *T,char fir,char sec){    AVL *s=&((*T)->child[fir]);    int button=fir*10+sec;    if(button==0 || button==11)    {        setfactor((*T),button);        conversion(T,fir);    }    else    {        setfactor((*T),button);        conversion(s,sec);        conversion(T,fir);    }}void selectforInsert(AVL *T,char *info,int direction){    AVL cur=*T;    char firdirection,secdirection;    if(direction==0)    (cur->bf)++;    else    (cur->bf)--;    if(cur->bf==0)    setbit(info,1,1);    else if(cur->bf==-1 || cur->bf==1)    setbit(info,direction,2);    else    {        firdirection=direction;        secdirection=getbit(*info,2);        keepbalance(T,firdirection,secdirection);        setbit(info,1,1);    }}char InsertAVL(AVL *T,Elemtype e,int id){    char info;    if(!(*T))    {        (*T)=createnode(e,id);        return 0;    }    else if((*T)->e==e)        return -1;    else if((*T)->e>e)    {        info=InsertAVL(&((*T)->child[0]),e,id);        if(getbit(info,1))    return info;        selectforInsert(T,&info,0);    }    else    {        info=InsertAVL(&((*T)->child[1]),e,id);        if(getbit(info,1))    return info;        selectforInsert(T,&info,1);    }    return info;}void find(AVL *q, int fight){    AVL p=*q;    AVL Q=*q;    int sum;    if(p->e > fight)sum = p->e - fight;    else   sum=fight - p->e;    while(p->child[0]!=NULL || p->child[1]!=NULL)    {        if(p->e > fight )        {            if(p->child[0]!=NULL)            {                p=(p)->child[0];                int ss;                if(fight > p->e ) ss= fight - p->e;                else    ss= p->e - fight;                if(sum > ss)                {                    sum = ss; Q=p;                }                if(sum==ss &&(Q->e > p->e))Q=p;            }            else              break;        }        else        {            if(p->child[1]!=NULL )            {                p=p->child[1];                int ss;                if(fight > p->e )ss= fight- p->e;                else             ss= p->e -fight;                if(sum>ss)                {                    sum=ss;                    Q=p;                }                if(sum==ss&&(Q->e > p->e)) Q=p;            }            else                break;        }    }    printf(" %d\n",Q->id);}int main(){    int i,nodenum;    scanf("%d",&nodenum);    while(nodenum!=0)    {        AVL T = NULL;        int id,fight;        InsertAVL(&T,INF,1);        for(i=2; i<=nodenum+1; i++)        {            scanf("%d%d",&id,&fight);            printf("%d",id);            find(&T,fight);            InsertAVL(&T,fight,id);        }        scanf("%d",&nodenum);    }    return 0;}