码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ】1588: [HNOI2002]营业额统计

时间:2017-02-10 21:48:01      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:ica   com   lock   time   lin   namespace   ted   ons   main   

【算法】平衡树(treap)||双向链表

【题解】

技术分享
#include<cstdio>
#include<algorithm>
#include<ctime>
using namespace std;
const int maxn=100010,inf=0x3f3f3f3f;
int n,sum,ans,sz,root;
struct cyc{int l,r,rnd,num;}t[maxn];
void insert(int &k,int x)
{
    if(k==0)
     {
         k=++sz;
         t[k].rnd=rand();
         t[k].num=x;
         return;
     }
    ans=min(ans,abs(t[k].num-x));
    if(x==t[k].num)return;
    if(x<t[k].num)insert(t[k].l,x);
     else insert(t[k].r,x);
}
int main()
{
    scanf("%d",&n);
    int x;
    for(int i=1;i<=n;i++)
     {
         ans=inf;
         scanf("%d",&x);
         insert(root,x);
         if(i==1)sum+=x;
          else sum+=ans;
     }
    printf("%d",sum);
    return 0;
}
View Code

 

【BZOJ】1588: [HNOI2002]营业额统计

标签:ica   com   lock   time   lin   namespace   ted   ons   main   

原文地址:http://www.cnblogs.com/onioncyc/p/6387782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!