求调昨晚cf的A
  • 板块灌水区
  • 楼主Zhang_Wenjie
  • 当前回复10
  • 已保存回复10
  • 发布时间2022/11/27 10:16
  • 上次更新2023/10/27 01:16:35
查看原帖
求调昨晚cf的A
481621
Zhang_Wenjie楼主2022/11/27 10:16

我用的贪心,简述就是尽量把所有重复出现过至少两次的数字包含在 [l,r][l,r] 里。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t,n,a[N],b[N];
bool same[N];
void init()
{
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(same,0,sizeof(same));
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int bn=-1;
		for(int i=1;i<=n;i++) 
		{
			cin>>a[i];
			b[a[i]]++;
			if(a[i]>bn) bn=a[i];
		}
		for(int i=1;i<=bn;i++)
		{
			if(b[i]>1) same[i]=true;
		}
		int l=1,r=n;
		bool lstop=false,rstop=false;
		while(l<r)
		{
			if(lstop && rstop) break;
			if(same[a[l]])
			{
				lstop=true;
			}
			if(same[a[r]])
			{
				rstop=true;
			}
			if(!lstop) l++;
			if(!rstop) r--;
		}
		cout<<l<<' '<<r<<endl;
	}
	return 0;
}
2022/11/27 10:16
加载中...