1520E

货仓选址变形题。

将所有星号聚拢到中间的星号上,总花费最小。

移动每个星号的花费是其与中间星号间的距离并减去二者之间的星号数量。

const int N = 1e6+10;
char s[N];
int a[N];
int n;
 
int main() {
    int T;
    cin >> T;
    while(T--) {
        cin>>n>>s;
 
        int cnt=0;
        for(int i=0;i<n;i++)
            if(s[i] == '*')
                a[cnt++]=i;
 
        int mid=a[cnt/2];
        LL res=0;
        for(int i=0;i<cnt;i++)
            res+=(abs(a[i]-mid)-1)-(abs(i-cnt/2)-1);
 
        cout<<res<<endl;
    }
    //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14803866.html