1588: [HNOI2002]營業額統計 splay 入門

來源:互聯網
上載者:User
 splay tree 伸展樹 推薦論文   伸展樹的基本操作與應用  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

該題資料bug已修複.----2016.5.15

Source

[ Submit][ Status][ Discuss] 網上找的代碼參考敲的 地址  http://spaceq.is-programmer.com/posts/39746.html ACcode:

#include <bits/stdc++.h>#define maxn 1000010#define inf 1e9#define lc(x) ch[(x)][0]#define min(x,y) (x)>(y)?(y):(x)using namespace std;int fa[maxn],ch[maxn][2],k[maxn],root,ind=1;inline void rotate(int p){    int q=fa[p],y=fa[q],x=ch[q][1]==p;    ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;    ch[p][x^1]=q;fa[q]=p;    fa[p]=y;    if(y){        if(ch[y][0]==q)ch[y][0]=p;        else if(ch[y][1]==q)ch[y][1]=p;    }}inline void splay(int x ){    for(int y;y=fa[x];rotate(x))        if(fa[y])            rotate((x==lc(y))==(y==lc(fa[y]))?y:x);    root=x;}inline void insert(int x,int v){    int y;    while(true){        y=ch[x][k[x]<v];        if(y==0){            y=++ind;            k[y]=v;            ch[y][0]=ch[y][1]=0;            fa[y]=x;            ch[x][k[x]<v]=y;            break;        }        x=y;    }splay(y);}inline int pre(int x){    int tmp=ch[x][0];    while(ch[tmp][1])tmp=ch[tmp][1];    return k[tmp];}inline int suc(int x){    int tmp=ch[x][1];    while(ch[tmp][0])tmp=ch[tmp][0];    return k[tmp];}int n,t,ans=0;inline void init(){     root=1;ans=k[root]=t;    fa[root]=ch[root][0]=ch[root][1]=0;    insert(root,inf);insert(root,-inf);}int main(){    ios::sync_with_stdio(false);    while(cin>>n){        cin>>t; init();        for(int i=2;i<=n;i++){            cin>>t; insert(root,t);            ans+=min(t-pre(root),suc(root)-t);        }        cout<<ans<<'\12';    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.