跳到正文

个人记录

题解 P4305 [JLOI2011]不重复数字

Chen_Qing约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月19日
篇幅
约 2 分钟
获赞
1

版本

  1. v1当前

P4305 [JLOI2011]不重复数字

第一次发题解。看到这个题的题解里面居然都搞得好复杂,居然没人用set!

拿到这个题第一印象好简单 一个set不就解决的,自动去重嘛。但是也自动排序呀。

但是~嘿嘿!为啥要去输出这个set呢,只是用它判断一下是不是重复就行了嘛!

于是

C++
#include<bits/stdc++.h>
using namespace std;
set<int>s;
pair<set<int>::iterator,bool> ret;

看到这个pair了,是不是想到了什么?

set的insert()方法呀,有返回值,插入后如果ret.second为真说明插入成功,也就是说这个数不重复,随即输出它。OK结束了!高兴的提交。 第一用的cin后四个超时了!加个快读OK 全部AC!

AC代码:

C++
#include<bits/stdc++.h>
using namespace std;
set<int>s;
pair<set<int>::iterator,bool> ret;
int t;

inline int read()
{
    int q=1,num=0;
    char ch=' ';
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')q=-1,ch=getchar();
    while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
    return num*q;
}

int main()
{
	t=read();
	while(t--)
	{
		int n;
		n=read();
		s.clear();
		for(int i=1;i<=n;++i)
		{
			int x=read();
			ret=s.insert(x);
			if(ret.second)cout<<x<<" ";
		}
		cout<<endl;
	}
	return 0;
}
原文
抓取于
  1. carrotpp

    大神阿