zoukankan      html  css  js  c++  java
  • bzoj1058 [ZJOI2007]报表统计

    题目大意 :给定一个序列,有多次询问和修改,将新元素插在原数列位置的后面,询问相邻元素差值最小,和数列中两元素最小的差。

    STL解法:用一个set去维护相邻两个数差的最小值,用一个堆去维护所有序列中最小的差

    #include<bits/stdc++.h>
    using namespace std;
    #define inf 1000000000
    #define maxn 500005
    int ed[maxn],st[maxn];//记录序列中每个位置的第一个和最后一个元素 
    int n,m;
    multiset<int>a,b;
    map<int,int>mp;
    priority_queue< int,vector<int>,greater<int> > k;
    inline int read()//加了快读才能在bzoj卡过去 
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    
    void insert(int x){
        mp[x]++;
        if(mp[x]==1)a.insert(x);
    } 
    void push(int x){
        int l=*--b.lower_bound(x),r=*b.lower_bound(x);//求x的前驱和后继,比较最小值。 
        k.push(min(x-l,r-x));
        b.insert(x);
    }
    int main(){
        n=read();m=read();
        b.insert(inf);//不要忘记初始化 
        b.insert(-inf);
        int x;
        for(int i=1;i<=n;i++){
        x=read();
        ed[i]=st[i]=x;
        push(x);
        }
        for(int i=2;i<=n;i++){
            insert(abs(st[i]-st[i-1]));
        }
        char s[15];int q,y;
        for(int i=1;i<=m;i++){
            scanf("%s",s);
            if(s[0]=='I'){
                q=read();y=read();
                if(q!=n){
                    int t=abs(ed[q]-st[q+1]);
                    mp[t]--;
                    if(!mp[t])a.erase(t);
                }
                insert(abs(ed[q]-y));
                insert(abs(y-st[q+1]));
                ed[q]=y;push(ed[q]);
            }
            else if(s[4]=='S'){
                printf("%d
    ",k.top() );
            }
            else printf("%d
    ",*a.begin());
        }
        return 0;
    }
  • 相关阅读:
    Python 字符串操作
    Python 字典操作
    16 飞机大战:思路整理、重要代码
    15 飞机大战:pygame入门、python基础串连
    14 windows下安装pygame模块
    13 制作模块压缩包、安装模块
    12 包及导包
    11 模块、模块的搜索顺序、__file__内置属性、__name__属性
    异常集
    10 异常
  • 原文地址:https://www.cnblogs.com/Elfish/p/7606633.html
Copyright © 2011-2022 走看看