zoukankan      html  css  js  c++  java
  • bzoj 2761: [JLOI2011]不重复数字 (map||Treap)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2761

    思路: map标记

    实现代码:

    #include<bits/stdc++.h>
    using namespace std;
    map<int,int>mp;
    int main()
    {
        int t,n,x;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            for(int i = 1;i <= n;i ++){
                scanf("%d",&x);
                mp[x]++;
                if(mp[x]==1) printf("%d ",x);
            }
            printf("
    ");
            mp.clear();
        }
    }

     这道题也可以用平衡树写,虽然也很水/////

    Treap写法:

    实现代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int M = 1e5 +10;
    struct node{
        int l,r,siz,cnt,wt,val;
    }t[M];
    int cnt,flag;
    
    void lt(int &k){
        int tmp = t[k].r;
        t[k].r = t[tmp].l;
        t[tmp].l = k;
        t[tmp].siz = t[k].siz;
        t[k].siz = t[t[k].l].siz + t[t[k].r].siz + t[k].cnt;
        k = tmp;
    }
    
    void rt(int &k){
        int tmp = t[k].l;
        t[k].l = t[tmp].r;
        t[tmp].r = k;
        t[tmp].siz = t[k].siz;
        t[k].siz = t[t[k].l].siz + t[t[k].r].siz + t[k].cnt;
        k = tmp;
    }
    
    void Insert(int &k,int num){
        if(!k){
            k = ++cnt;
            t[k].wt = rand();
            t[k].val = num;
            t[k].cnt = t[k].siz = 1;
            return ;
        }
        ++t[k].siz;
        if(num == t[k].val){
            ++t[k].cnt;
            flag = 1;
            return ;
        }
        if(num < t[k].val){
            Insert(t[k].l,num);
            if(t[t[k].l].wt < t[k].wt) rt(k);
        }
        else{
            Insert(t[k].r,num);
            if(t[t[k].r].wt < t[k].wt)
                lt(k);
        }
    }
    
    int main()
    {
        int T,n,x,root;
        scanf("%d",&T);
        while(T--){
            memset(t,0,sizeof(t));
            root = cnt = 0;
            scanf("%d",&n);
            for(int i = 1;i <= n;i ++){
                cin>>x;
                flag = 0;
                Insert(root,x);
                if(!flag) printf("%d ",x);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    002-html表格
    001-html常见的标记
    获取本地内外网ip地址
    Windows10设置默认简体美式键盘输入法
    C# 历史版本特性变更
    SQL Server Report Builder RDLC按记录数分页
    自动补全(备份)
    t:datagrid 行编辑 类型备份
    自动生成编号
    文本框上绑校验
  • 原文地址:https://www.cnblogs.com/kls123/p/10574246.html
Copyright © 2011-2022 走看看