zoukankan      html  css  js  c++  java
  • BZOJ 1208

    这道题可以用set水过,也可以练习写一下treap...
    显然任何时候,领养人和宠物都不会同时存在……
    所以就相当于每次查询序列的前驱/后继了…
    贴上两份代码吧…

    代码一(set):

    // BZOJ 1208 set
    
    #include <cstdio>
    #include <set>
    #include <cstring>
    using namespace std;
    
     #define rep(i,a,b) for (int i=a; i<=b; i++)
     #define read(x) scanf("%d", &x)
     #define mod 1000000
     #define INF 0x7fffffff
    
     set<int> S;
     int n, ans=0, a, b, exist;
     bool flag;
    
    int main()
    {
    	S.insert(INF); S.insert(-INF);
    	read(n);
    	rep(i,1,n) {
    		read(a); read(b);
    		if (S.size()==2) { // 如果S为空
    			exist=a;
    			S.insert(b);
    		}
    		else if (a!=exist) { 
    			int l=*--S.lower_bound(b); // 求前驱
    			int r=*S.lower_bound(b); // 求后继(奇怪的语法… 不要在意细节)
    			if (b-l<=r-b && l>-INF) {
    				ans+=(b-l);
    				S.erase(l); // set的删除是erase!
    			}
    			else {
    				ans+=(r-b);
    				S.erase(r);
    			}
    			ans%=mod;
    		}
    		else S.insert(b);
    	}
        printf("%d
    ", ans);
    
        return 0;
    }
    
    

    代码二(treap):

    // BZOJ 1208 treap
    
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
     #define rep(i,a,b) for (int i=a; i<=b; i++)
     #define read(x) scanf("%d", &x)
     #define mod 1000000
     #define INF 0x7fffffff
    
     struct Node {
     	Node *son[2];
     	int v, p, s;
     	int cmp(int x) {
     		if (x==v) return -1;
     		if (x<v) return 0;
     		else return 1; 
     	}
     	void maintain() { s = son[1]->s + son[0]->s + 1; }
     } *root;
    
     Node *null = new Node();
    
     void rotate(Node *&o, int d) {
     	Node *k = o->son[d^1];
     	o->son[d^1] = k->son[d];
     	k->son[d] = o;
     	o->maintain();
     	k->maintain();
     	o = k;
     }
    
     void insert(Node *&o, int x) {
     	if (o==null) {
     		o = new Node();
     		o->v = x; o->p = rand();
     		o->son[0] = o->son[1] = null;
     	}
     	else {
     		int d = (x < o->v ? 0 : 1);
     		insert(o->son[d], x);
     		if (o->son[d]->p > o->p) rotate(o, d^1);
     	}
     	o->maintain();
     }
    
     void remove(Node *&o, int x) {
     	if (o==null) return;
     	int d = o->cmp(x);
     	if (d==-1) { // 找到了,准备删
     		Node *u = o;
     		if (o->son[0] != null && o->son[1] != null) { // 如果两棵子树均不为空
     			int d2 = (o->son[0]->p > o->son[1]->p ? 1 : 0); 
     			rotate(o, d2); // 将优先级较大的那一棵子树转到根节点上,递归删除
     			remove(o->son[d2], x);
     		}
     		else {
     			if (o->son[0] == null) o = o->son[1]; else o = o->son[0];
     			delete u;
     		}
     	} else remove(o->son[d], x); // 递归删除
     	if (o!=null) o->maintain(); // 别忘了删完了立即维护信息
     }
    
     int query_pre(int x) {
     	Node *o = root;
     	int ret=0;
     	while (o!=null) 
     		if (o->v < x) {
     			ret = o->v;
     			o = o->son[1];
     		} 
     		else o = o->son[0];
     	return ret;
     }
    
     int query_suf(int x) {
     	Node *o = root;
     	int ret=0;
     	while (o!=null) 
     		if (o->v > x) {
     			ret = o->v;
     			o = o->son[0];
     		} 
     		else o = o->son[1];
     	return ret;
     }
    
     void init() {
     	null->s = 0;
     	root = null;
     	insert(root, INF);
        insert(root, -INF);
     }
    
    int main()
    {
    	int n=2, ans=0, m, a, b, exist;
    	read(m);
    	init();
    	while (m--) {
    		read(a); read(b);
    		if (n==2) { insert(root, b); exist=a; n=3; }
    		else if (exist==a) { insert(root, b); n++; }
    		else {
    			int l=query_pre(b), r=query_suf(b);
    			if (b-l<=r-b && l!=-INF) {
    				ans+=(b-l);
    				remove(root, l);
    				n--;
    			}
    			else {
    				ans+=(r-b);
    				remove(root, r);
    				n--;
    			}
    			ans%=mod;
    		}
    	}
    	printf("%d
    ", ans);
    
    	return 0;
    }
    

    BZOJ的测试结果显示窝手写的treap比set还慢了4ms... 身败名裂...

  • 相关阅读:
    数据库三大范式
    sql 外键 on update cascade 和 on delete cascade 作用区别?
    Mybatis入门简版(二)
    Mybatis入门简版(一)
    Mybatis入门简版(补充)
    SQL中ON和WHERE的区别
    MySQL基础(五)常见运算符
    MySQL基础(四)常用函数
    MySQL基础(三)多表查询(各种join连接详解)
    MySQL基础(二)
  • 原文地址:https://www.cnblogs.com/yearwhk/p/5134844.html
Copyright © 2011-2022 走看看