首页 > 其他 > 详细

bzoj 2761: [JLOI2011]不重复数字

时间:2019-03-21 21:05:19      阅读:152      评论:0      收藏:0      [点我收藏+]

链接: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("\n");
        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("\n");
    }
    return 0;
}

 

bzoj 2761: [JLOI2011]不重复数字

原文:https://www.cnblogs.com/kls123/p/10574246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!