求助,不知为什么WA了
查看原帖
求助,不知为什么WA了
158652
IQ勇士楼主2022/8/3 10:03

我的思路是排序之后OnO(n)扫描,用一个数组存储当前出现最多的数,用maxx存储当前出现最多的数的出现次数,最后再特判-1的情况,但是WA了

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int T;
int a[100001], n, maxx, now;
struct stackk{
	int data[100001];
	int top;
	void csh()
	{
		top = 0;
	}
	void push(int x)
	{
		data[++top] = x;
	}
}st;
int main()
{
	cin >> T;
	while(T--)
	{
		st.csh();
		maxx = -1;
		cin >> n;
		for(int i = 1; i <= n; i++)
			cin >> a[i];
		sort(a + 1, a + n + 1);
		for(int i = 1; i <= n; i++)
		{
			if(i == 1 || (a[i - 1] != a[i]))
				now = 1;
			else
				now++;
			if(i == n || a[i + 1] != a[i])
			{
				if(now > maxx)
				{
					maxx = now;
					st.csh();
					st.push(a[i]);
				}
				else if(now == maxx)
					st.push(a[i]);
			}
		}
		if(maxx * st.top == n)
			cout << -1;
		else
		{
			cout << st.top << endl;
			for(int i = 1; i <= st.top; i++)
				cout << st.data[i] << ' ';
			cout << endl;
		}
	}
	return 0;
}
2022/8/3 10:03
加载中...