题意:
找出有几个数量相等的连续的区间对。 区间里的相对距离<=V.
POINT:
n^2的效率遍历上下数组的开头。然后每次扫一遍就有答案。因为区间要连续的。每次都是右边加进来。左边删掉最优。
注意不要重复。
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
LL a[1111],b[1111];
LL d[1111][1111];
LL dis(LL x,LL y,LL p)
{
LL temp = abs(x-y);
LL ans = 1;
while(p--){
ans*=temp;
}
return ans;
}
int main()
{
LL T;
scanf("%lld",&T);
LL n,V,p;
while(T--){
scanf("%lld %lld %lld",&n,&V,&p);
for(LL i=1;i<=n;i++) scanf("%lld",&a[i]);
for(LL i=1;i<=n;i++) scanf("%lld",&b[i]);
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++){
d[i][j]=dis(a[i],b[j],p);
}
}
queue<LL> q;
LL ans = 0;
LL flag[3333];
memset(flag,0,sizeof flag);
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++){
if(flag[i-j+n]) continue;
flag[i-j+n]=1;
LL ti=i,tj=j;
LL now = 0;
while(ti<=n&&tj<=n){
while(now+d[ti][tj]>V){
if(!q.empty()){
now-=q.front();
q.pop();
}else{
break;
}
}
if(now+d[ti][tj]<=V){
q.push(d[ti][tj]);
now+=d[ti][tj];
}
ans+=q.size();
ti++,tj++;
}
while(!q.empty()) q.pop();
}
}
printf("%lld\n",ans);
}
}