BZOJ 1588 [HNOI2002]营业额统计

来源:互联网 发布:随心所欲动作数据 编辑:程序博客网 时间:2024/05/21 05:22

treap的入门题,虽然在splay的论文里看到过这题。。。

遇到2个奇葩问题:1:BZOJ 上用srand(time(NULL))会RE 2:这题的数据不完整

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 6820  Solved: 2250
[Submit][Status]

Description

营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。  输入输出要求

Input

第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个正整数 ,表示第i天公司的营业额。

Output

输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

Sample Input

6
5
1
2
5
4
6

Sample Output

12

HINT

结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12

Source

[Submit][Status]


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <ctime>using namespace std;const int INF=0x3f3f3f3f;struct Node{    int r,v,s;    Node* ch[2];    int cmp(int x)    {        if(v==x) return -1;        return (x<v)?0:1;    }    void maintain()    {        s=ch[0]->s+1+ch[1]->s;    }};Node* nill;void init(){    nill=new Node();    nill->s=0;    nill->ch[0]=nill->ch[1]=nill;    //srand(time(NULL));}void rotate(Node* &o,int d){    Node* k=o->ch[1^d];    o->ch[1^d]=k->ch[d];    k->ch[d]=o;    o->maintain();    k->maintain();    o=k;}void insert(Node* &o,int x){    if(o==nill)    {        o=new Node();        o->v=x;o->r=rand();o->s=1;        o->ch[0]=o->ch[1]=nill;    }    else    {        int d=o->cmp(x);        if(d==-1) d=0;        insert(o->ch[d],x);        if(o->ch[d]->r>o->r) rotate(o,1^d);    }    o->maintain();}void remove(Node* &o,int x){    int d=o->cmp(x);    if(d==-1)    {        Node* u=o;        if(o->ch[0]!=nill&&o->ch[1]!=nill)        {            int d2=(o->ch[0]->r>o->ch[1]->r)?1:0;            rotate(o,d2);            remove(o->ch[d2],x);        }        else        {            if(o->ch[1]==nill) o=o->ch[0]; else o=o->ch[1];            delete u;        }    }    else    {        remove(o->ch[d],x);    }    if(o!=nill) o->maintain();}int find(Node* o,int x){    while(o!=nill)    {        int d=o->cmp(x);        if(d==-1) return 1;        o=o->ch[d];    }    return 0;}int Kth(Node* o,int x)///kth small{    while(o!=nill)    {        int s=o->ch[0]->s;        if(x==s+1) return o->v;        else if(x<=s) o=o->ch[0];        else        {            o=o->ch[1];x=x-s-1;        }    }    return -INF;}int Rank(Node* o,int x){    int ret=0;    while(o!=nill)    {        int s=o->ch[0]->s;        if(o->v<=x)        {            ret+=s+1;            o=o->ch[1];        }        else        {            o=o->ch[0];        }    }    return ret;}int main(){    int n;while(scanf("%d",&n)!=EOF){    int ret;    scanf("%d",&ret);    init();    Node* root=nill;    insert(root,ret);    for(int i=2;i<=n;i++)    {        int a,temp1=-INF,temp2=INF;        if(scanf("%d",&a)==EOF) a=0;        int rk=Rank(root,a);        if(rk) temp1=Kth(root,rk);        if(rk+1<=i-1) temp2=Kth(root,rk+1);        ret+=min(a-temp1,temp2-a);        insert(root,a);    }    printf("%d\n",ret);}    return 0;}





1 0
原创粉丝点击