• bzoj1058: [ZJOI2007]报表统计


    set。操作:insert(u,v)在u后面插入v,若u后面已插入过,在插入过的后面插入。mingap求出序列两两之间差值的最小值。minsortgap求出排序后的序列两两之间的最小值。用multiset维护就可以了。忽略了新插入的数对于mingap的影响WA了一次。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<set>
    #include<queue>
    using namespace std;
    #define rep(i,s,t) for(int i=s;i<=t;i++)
    int read(){
    	int x=0;char c=getchar();bool f=true;
    	while(!isdigit(c)) {
    		if(c=='-') f=false;c=getchar();
    	}
    	while(isdigit(c)) x=x*10+c-'0',c=getchar();
    	return f?x:-x;
    }
    const int nmax=500005;
    const int inf=0x7f7f7f7f;
    int a[nmax],last[nmax];
    char s[20];
    multiset<int>S,T;
    int as(int x){
    	return x<0?-x:x;
    }
    int main(){
    	int n=read(),m=read();
    	rep(i,1,n) a[i]=read(),last[i]=a[i],S.insert(a[i]);
    	S.insert(-inf);S.insert(inf);
    	
    	int smin=inf;
    	set<int>::iterator it;
    	set<int>::iterator tmp;
    	for(it=S.begin();it!=S.end();it++){
    		if(it!=S.begin()) smin=min(smin,*it-*tmp);
    		tmp=it;
    	}
    	
    	rep(i,2,n) T.insert(as(a[i]-a[i-1]));
    	
    	set<int>::iterator first;
    	set<int>::iterator second;
    	while(m--){
    		scanf("%s",s);
    		if(s[0]=='I'){
    			int u=read(),v=read();
    			it=T.find(as(a[u+1]-last[u]));
    			T.erase(it);T.insert(as(v-last[u]));T.insert(as(a[u+1]-v));
    			last[u]=v;
    			
    			it=S.insert(v);first=--it;++it;second=++it;--it;
    			smin=min(smin,min(v-*first,*second-v));
    		}else if(s[4]=='G') printf("%d
    ",*T.begin());
    		else printf("%d
    ",smin);
    	}
    	return 0;
    }
    

    ps:set用法

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<set>
    using namespace std;
    int read(){
    	int x=0;char c=getchar();
    	while(!isdigit(c)) c=getchar();
    	while(isdigit(c)) x=x*10+c-'0',c=getchar();
    	return x;
    }
    set<int>s;
    int main(){
    	int n=read();
    	for(int i=1;i<=n;i++) {
    		int tmp=read();s.insert(tmp);
    	}
    	set<int>::iterator it;
    	for(it=s.begin();it!=s.end();it++)
    	 printf("%d
    ",*it);
    	printf("%d
    ",*s.begin());
    	printf("%d
    ",*s.end());
    	printf("%d
    ",s.size());
    	printf("%d
    ",s.count(2));//0/1
    	printf("%d
    ",s.count(5));
    	s.clear();
    	if(s.empty()) printf("Orzzzzzz
    ");
    	
    	printf("erase operator
    ");
    	set<int>::iterator first;
    	set<int>::iterator second;
    	for(int i=1;i<=10;i++) s.insert(i);
    	first=s.begin();second=s.begin();second++;second++;
    	s.erase(first, second);s.erase(s.begin());s.erase(7);
    	for(it=s.begin();it!=s.end();it++)
    	  printf("%d
    ",*it);
    	  
    	printf("find operator
    ");
    	s.clear();
    	for(int i=1;i<=10;i++) s.insert(i);
    	if((it=s.find(5))!=s.end()) printf("%d
    ",*it);//qaq what function
    	printf("%d
    ",*(++it));
    	
    	printf("wzc operator
    ");
    	printf("%d
    ",*s.lower_bound(3));
    	printf("%d
    ",*s.upper_bound(3));
    	return 0;
    }
    

      

    1058: [ZJOI2007]报表统计

    Time Limit: 15 Sec  Memory Limit: 162 MB
    Submit: 2815  Solved: 968
    [Submit][Status][Discuss]

    Description

      小Q的妈妈是一个出纳,经常需要做一些统计报表的工作。今天是妈妈的生日,小Q希望可以帮妈妈分担一些工
    作,作为她的生日礼物之一。经过仔细观察,小Q发现统计一张报表实际上是维护一个可能为负数的整数数列,并
    且进行一些查询操作。在最开始的时候,有一个长度为N的整数序列,并且有以下三种操作: INSERT i k 在原数
    列的第i个元素后面添加一个新元素k; 如果原数列的第i个元素已经添加了若干元素,则添加在这些元素的最后(
    见下面的例子) MIN_GAP 查询相邻两个元素的之间差值(绝对值)的最小值 MIN_SORT_GAP 查询所有元素中最接
    近的两个元素的差值(绝对值) 例如一开始的序列为 5 3 1 执行操作INSERT 2 9将得到: 5 3 9 1 此时MIN_GAP
    为2,MIN_SORT_GAP为2。 再执行操作INSERT 2 6将得到: 5 3 9 6 1 注意这个时候原序列的第2个元素后面已经
    添加了一个9,此时添加的6应加在9的后面。这个时候MIN_GAP为2,MIN_SORT_GAP为1。于是小Q写了一个程序,使
    得程序可以自动完成这些操作,但是他发现对于一些大的报表他的程序运行得很慢,你能帮助他改进程序么?

    Input

      第一行包含两个整数N,M,分别表示原数列的长度以及操作的次数。第二行为N个整数,为初始序列。接下来
    的M行每行一个操作,即“INSERT i k”,“MIN_GAP”,“MIN_SORT_GAP”中的一种(无多余空格或者空行)。

    Output

      对于每一个“MIN_GAP”和“MIN_SORT_GAP”命令,输出一行答案即可。

    Sample Input

    3 5
    5 3 1
    INSERT 2 9
    MIN_SORT_GAP
    INSERT 2 6
    MIN_GAP
    MIN_SORT_GAP

    Sample Output

    2
    2
    1

    HINT

    N , M ≤500000 对于所有的数据,序列内的整数不超过5*10^8。

    Source

     
    [Submit][Status][Discuss]
  • 相关阅读:
    C#使用枚举方法来实现读取用户电脑中安装的软件
    C#实现窗体阴影效果
    [转]SQLserver字符串分割函数
    [转]WinForm实现win7 Aero磨砂效果介绍
    C#通过鼠标点击panel移动来控制无边框窗体移动
    [转]Table交替行变色 鼠标经过变色 单击变色
    《面向对象程序设计》 三 Calculator 计算器初步
    PAT 1001A+B Format
    大一下学期的自我目标
    《面向对象程序设计》第二次作业
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5725573.html
Copyright © 2020-2023  润新知