• 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

  • 相关阅读:
    小程序开发经验分享汇总
    VUE插件总结
    前端开发中一些好用的chrome插件总结
    Vue-meta
    element-ui Cascader 级联选择器 点击label选中
    盘点一些跨端框架
    vue 组件自定义v-model
    mac更新node,npm版本
    数组/对象数组去重
    基于vue和svg的树形UI
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5769449.html
Copyright © 2020-2023  润新知