思路;先找到第一个包含m家店的区间(l-r),然后开始进行双指针(l,r)滑动(如下滑动)
while(r<=n)
{
while(vis[a[l]]>1)//当前l-r之间a[l]店铺有多个(大于一个),那即可去掉当前l
{
vis[a[l]]--;
l++;
}
if(ans>=(c[r]-c[l-1]))
{
ansl=l,ansr=r,ans=c[r]-c[l-1];
}
r++;
vis[a[r]]++;
}
ACcode:
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define ll long long
int n,m;
int ansl,ansr;
ll a[N],b[N],c[N],vis[N];
ll ans;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
{
cin>>b[i];
c[i]=c[i-1]+b[i];
}
int l=1,r=0,cnt=0;
ll sum=0;
while(r<=n)
{
r++;
if(!vis[a[r]])cnt++;
vis[a[r]]++;
if(cnt==m)break;
}
ansl=l,ansr=r,ans=c[r];
while(r<=n)
{
while(vis[a[l]]>1)
{
vis[a[l]]--;
l++;
}
if(ans>=(c[r]-c[l-1]))
{
ansl=l,ansr=r,ans=c[r]-c[l-1];
}
r++;
vis[a[r]]++;
}
cout<<ansl<<" "<<ansr<<" "<<ans;
return 0;
}
over~