5524.多数意见
题目链接:https://www.acwing.com/problem/content/description/5527/
题意:
给定一个长度为n的数组,可以进行任意次操作
每次操作选取一段连续的区间,如果这个区间中有超过一半的相同的数字,便可以将区间中其他的数字同化为该数字
试求有哪些数字可以将区间完全同化,若无,输出-1
思路:
考察数组的第i个元素,假设它为x
如果x相邻一格也有x,那么即使相邻两格不是x,也能通过操作使其同化(2/3),进而同化所有元素
如果x相邻一格没有x,而相邻两格有x,同样能同化(2/3)
发现再扩大区间,即使相邻三格有x,也无法同化夹在中间的两个元素(2/4)
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define endl "\n"
#define fi first
#define se second
//#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
const int mod=1e9+7;
int n;
int h[maxn];
signed main()
{
ios::sync_with_stdio(false),cin.tie(0);
int T;cin>>T;
while(T--){
cin>>n;
rep(i,1,n) cin>>h[i];
set<int>ans;
for(int i=1;i<=n;i++){
int x=h[i];
if(i-2>=1&&h[i-2]==x)ans.insert(x);
if(i-1>=1&&h[i-1]==x)ans.insert(x);
if(i+1<=n&&h[i+1]==x)ans.insert(x);
if(i+2<=n&&h[i+2]==x)ans.insert(x);
}
if(ans.size()==0){
cout<<-1<<endl;
continue;
}
for(auto res:ans){
cout<<res<<' ';
}
cout<<endl;
}
return 0;
}

浙公网安备 33010602011771号