zoukankan      html  css  js  c++  java
  • 洛谷 P4305 [JLOI2011]不重复数字

    题目链接:[JLOI2011]不重复数字

    题解原发于我的blog

    两个月不写题解了,今天来水一波

    这题的目标就是去重,我们都知道c++有STL

    先把整个序列按值排序一遍,在按值去重,再按原来的顺序排回去,就这么简单

    注意使用(sort)时可能会打乱前后顺序,所以也要在排序时加入另外加入与位置有关的条件
    复杂度(O(nlogn))

    #include<cstdio>
    #include<algorithm>
    #define re register
    using namespace std;
    template <typename T>
    inline void read(T &x)
    {
    	x=0;
    	char s=(char)getchar();
    	bool flag=false;
    	while(s<'0'||s>'9')
    	{
    		if(s=='-')
    			flag=true;
    		s=(char)getchar();
    	}
    	while(s>='0'&&s<='9')
    	{
    		x=(x<<1)+(x<<3)+(s^'0');
    		s=(char)getchar();
    	}
    	if(flag)
    		x=~x+1;
    	return;
    }
    const int N=5e4+5;
    int n;
    struct node
    {
    	int val,id;
    	inline bool operator <(const node &rhs)const
    	{
    		return val<rhs.val||val==rhs.val&&id<rhs.id;
    	}
    	inline bool operator ==(const node &rhs)const
    	{
    		return val==rhs.val;
    	}
    } a[N];
    inline bool cmp(const node &x,const node &y)
    {
    	return x.id<y.id;
    }
    int main()
    {
    	int T;
    	read(T);
    	while(T--)
    	{
    		read(n);
    		for(re int i=1; i<=n; ++i)
    		{
    			read(a[i].val);
    			a[i].id=i;
    		}
    		sort(a+1,a+1+n);
    		n=unique(a+1,a+1+n)-1-a;
    //		printf("%d
    ",n);
    		sort(a+1,a+1+n,cmp);
    		for(re int i=1; i<=n; ++i)
    			printf("%d ",a[i].val);
    		putchar('
    ');
    	}
    	return 0;
    }
    
  • 相关阅读:
    OSX中zsh新增环境变量
    新的开始 春光明媚
    tmux
    继承
    6
    Object类
    网页收藏
    画王八
    ES6 语法之import export
    ES6 语法 之 destructuring
  • 原文地址:https://www.cnblogs.com/wangjunrui/p/12235450.html
Copyright © 2011-2022 走看看