题目:
题目链接:
题解:
y
y
y相同,
x
x
x不同
可以参考货舱选址问题,找到所有
x
x
x的中位数,求到中位数的距离,但由于最后
x
x
x不在同一个点,所以再减去
x
x
x的相对坐标即可,即
x
x
x -
i
i
i
#include <bits/stdc++.h>
using namespace std;
int b[1000005];
int main()
{
int t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
string a;
cin>>a;
int con=0;
for(int i=0;i<n;i++)
{
if(a[i]=='*') b[con++]=i+1;
}
for(int i=0;i<con;i++)
{
b[i]-=i+1;
}
int mid=b[(con-1)/2];
long long ans=0;
for(int i=0;i<con;i++)
{
ans+=abs(b[i]-mid);
}
cout<<ans<<endl;
}
return 0;
}