zoukankan      html  css  js  c++  java
  • bzoj1455罗马游戏*

    bzoj1455罗马游戏

    题意:

    维护数据结构支持合并和弹出最小值。n≤1000000,m≤100000

    题解:

    可并堆,注意本题合并时要判断两个节点是否在同一个堆中。本弱写了左偏树和斜堆,发现斜堆比左偏树快,不知道为什么,求神犇解答。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define inc(i,j,k) for(int i=j;i<=k;i++)
     5 #define maxn 1000010
     6 using namespace std;
     7 
     8 inline int read(){
     9     char ch=getchar(); int f=1,x=0;
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    12     return f*x;
    13 }
    14 int ch[maxn][2],v[maxn],fa[maxn]; bool die[maxn];
    15 int find(int x){while(fa[x])x=fa[x]; return x;}
    16 int merge(int x,int y){
    17     if(!x||!y)return x+y; if(v[x]>v[y])swap(x,y); ch[x][1]=merge(ch[x][1],y);
    18     fa[ch[x][1]]=x; swap(ch[x][0],ch[x][1]); return x;
    19 }
    20 void pop(int x){
    21     fa[ch[x][0]]=fa[ch[x][1]]=0; merge(ch[x][0],ch[x][1]); ch[x][0]=ch[x][1]=0;
    22 }
    23 int n,m; char opt[3];
    24 int main(){
    25     n=read(); inc(i,1,n)v[i]=read(); m=read();
    26     inc(i,1,m){
    27         scanf("%s",opt);
    28         if(opt[0]=='M'){
    29             int a=read(),b=read(); if(die[a]||die[b])continue; int aa=find(a),bb=find(b);
    30             if(aa==bb)continue; merge(aa,bb);
    31         }
    32         if(opt[0]=='K'){
    33             int a=read(); if(die[a]||a>n)puts("0");else{
    34                 int aa=find(a); pop(aa); die[aa]=1; printf("%d
    ",v[aa]);
    35             }
    36         }
    37     }
    38     return 0;
    39 }

    20160810

  • 相关阅读:
    关于MySQL数据库优化的部分整理
    PHP跨域form提交
    px、dp和sp,这些单位有什么区别?
    301和302 Http状态有啥区别?
    PHP HTTP请求
    php的http_build_query使用
    nginx ssi 模块
    MongoDB学习笔记(一) MongoDB介绍及安装(摘)
    Django Admin 录入中文错误解决办法
    关于python字符串连接的操作
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5769449.html
Copyright © 2011-2022 走看看