WAon#4求调悬1关
查看原帖
WAon#4求调悬1关
1170111
MCxiaokang楼主2025/1/23 16:18
#include<bits/stdc++.h>
#define ull unsigned long long
using namespace std;

const int maxn=2e4+10;
const ull mod1=1e9+5,p1=13331,mod2=1e18+1093,p2=13337;
int n,k,l,r,mid,ans;
int a[maxn];
ull h1[maxn],h2[maxn],pow_p1[maxn],pow_p2[maxn];//h[j]表示1~j的hsh
unordered_map<ull,ull> m1,m2;

ull max(ull a,ull b)
{
	if (a>b) return a;
	return b;
}

ull min(ull a,ull b)
{
	if (a<b) return a;
	return b;
}

//ull hsh(int j)
//{
//	ull tot=0;
//	for (ull i=1;i<=j;i++)
//	{
//		tot=(tot*p+(ull)a[i])%mod;
//	}
//	return tot;
//}

ull gethsh1(ull l,ull r)
{
	return (((h1[r]-h1[l-1]*pow_p1[r-l+1])%mod1)+mod1)%mod1;
}

ull gethsh2(ull l,ull r)
{
	return (((h2[r]-h2[l-1]*pow_p2[r-l+1])%mod2)+mod2)%mod2;
}

bool check(int len)
{
	m1.clear();m2.clear();
	ull ans1=0,ans2=0,hsh_1,hsh_2;
	for (ull i=1;i+len-1<=n;i++)
	{
		hsh_1=gethsh1(i,i+len-1);
		hsh_2=gethsh2(i,i+len-1);
		m1[hsh_1]++;
		m2[hsh_2]++;
		ans1=max(m1[hsh_1],ans1);
		ans2=max(m2[hsh_2],ans2);
	}
	cout<<"len&ans:"<<len<<" "<<ans1<<" "<<ans2<<"\n";
	return max(ans1,ans2)>=k;
}
//hsh[l,r]=hsh[1,r]-(hsh[1,l-1]*pow_p[r-l+1])

int main()
{
//	freopen("P2852_2.in","r",stdin);
//	freopen("P2852_2.ans","w",stdout);
	cin>>n>>k;
	for (int i=1;i<=n;i++)
	{
		cin>>a[i];
		a[i]++;
		h1[i]=(h1[i-1]*p1+a[i])%mod1;
		h2[i]=(h2[i-1]*p2+a[i])%mod2;
//		cout<<h[i]<<" ";
	}
	pow_p1[0]=pow_p2[0]=1;
	for (int i=1;i<=n;i++)
	{
		pow_p1[i]=pow_p1[i-1]*p1%mod1;
		pow_p2[i]=pow_p2[i-1]*p2%mod2;
	}
	l=0,r=n,ans=0;
	while(l<r)
	{
		cout<<l<<" "<<r<<"\n";
		mid=(l+r+1)>>1;
		if (check(mid)) l=mid;
		else r=mid-1;
	}
	cout<<l<<"\n";
	return 0; 
}

h1 h2 双哈希,感觉是二分的问题,但是调了好久都没法过。附#4数据:

20 2 3 3 3 2 2 2 3 3 3 2 2 3 3 1 2 3 2 3 2 3
5
2025/1/23 16:18
加载中...