Bzoj 1588 turnover statistics (splay entry into the search for the successor)

Source: Internet
Author: User
Tags abs
Topic links

BZOJ1588 Topic

Enter n N Number (n≤32767 n\le32767), each read into a number, in the previous input to find a number of the smallest difference. Add up all the difference to get the answer. Analysis

I wrote the first splay question, basically is the template of the great God to write.
Inserts a number into the stretch tree each time it is read, and then rotates it to the root node. The least-difference point is that the rightmost point in the left dial hand tree and the left-most-right sub-tree are mapped to a linear ordered array, which is its precursor and post-drive node. Code

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <
algorithm> #include <queue> #define LS (rt<<1) #define RS (rt<<1|1) using namespace std;
Const Double Pi=4*atan (1.0);
const int maxn=100010;

const int MAXM=2*MAXN;
    struct Splaytree {int root,tot;///root node, number of nodes int NT[MAXN][2],PRE[MAXN],VAL[MAXN];
    Indicate left and right children (0 left 1), parent node, key value void Init () {root=tot=0;}
        void Newnode (int &rt,int father,int value)///Create a new node {rt=++tot;
        nt[rt][0]=nt[rt][1]=0;
        Pre[rt]=father;
    Val[rt]=value;
        } void Rotate (int x,int kind)//rotation: Kind 0 is left, 1 is right-handed {int y=pre[x];
        Nt[y][!kind]=nt[x][kind];
        Pre[nt[x][kind]]=y;
        if (Pre[y]) {nt[pre[y]][nt[pre[y]][1]==y]=x;
        } Pre[x]=pre[y];
        Nt[x][kind]=y;
    Pre[y]=x;
} void splay (int x,int goal)///splay Adjustment: Adjust node X to goal below {while (pre[x]!=goal)        {if (pre[pre[x]]==goal)///single-spin case Rotate (X,NT[PRE[X]][0]==X);
                else {int y=pre[x];
                int kind=nt[pre[y]][0]==y;
                    if (nt[y][kind]==x)///The font rotation {Rotate (x,!kind);
                Rotate (X,kind);
                    } else///a font rotation {Rotate (y,kind);
                Rotate (X,kind);
    }}} if (!goal) root=x;
        } bool Insert (int k) {int rt=root;
                while (Nt[rt][val[rt]<k]) {if (val[rt]==k)///If the element itself exists, simply adjust it to the root node {
                Splay (rt,0);
            return false;
        } rt=nt[rt][val[rt]<k]; } Newnode (Nt[rt][val[rt]<k],rt,k);
        This element is not present on the newly inserted element splay (nt[rt][val[rt]<k],0);
    return true; } int GET_PRE (int rt)////Find the precursor node of RT node that is the rightmost node of the left subtree {int x=nt[rt][0];
            while (x) {while (nt[x][1]) x=nt[x][1];
        return val[x];
    } return-1;
        } int get_next (int rt)////Find the successor node of the RT node that is the leftmost node in the right subtree {int x=nt[rt][1];
            while (x) {while (nt[x][0]) x=nt[x][0];
        return val[x];
    } return-1;
}
};
Splaytree Tree;
    int main () {Ios::sync_with_stdio (false);
    int i,ans,num,n;
    Freopen ("In.txt", "R", stdin); while (scanf ("%d", &n)!=eof) {tree.
        Init ();
        ans=0;
            for (i=1;i<=n;i++) {scanf ("%d", &num);
                if (i==1) {ans+=num; Tree.
                Newnode (Tree.root,0,num);
            Continue } if (!tree.
            Insert (num)) continue;
            int A=tree.get_pre (tree.root); int B=tree.get_next (tree.root);   if (a==-1) ans+=abs (num-b);
            Be aware of the former and else if (b==-1) ans+=abs (num-a);
        Else Ans+=min (ABS (NUM-B), ABS (NUM-A));
    } printf ("%d\n", ans);
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.