๐Ÿ“ฆ ved015 / Codeforces-solution

๐Ÿ“„ arranginthesheep1400.cpp ยท 101 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101// #include<iostream>
// #include<vector>
// #include<algorithm>
// #include<map>
// #include<set>
// #include<string>
// using namespace std;
// int dist(string &s,int start){
//     int val = 0;
//     int idx = start;
//     for(int i = 0; i < s.size(); i++){
//         if(s[i] == '*'){  
//             val += abs(i - idx);
//             idx++;
//         }   
//     }
//     return val;
// }
// int main(){
//     int t;
//     cin >> t;
//     while(t--){
//         int n;
//         cin >> n;
//         string sheep;
//         cin >> sheep;
//         int total = 0;
//         for(auto &it : sheep){
//             if(it == '*') total++;
//         }
//         total--;
//         // {sheep_count,{start,end}}
//         vector<pair<int,pair<int,int> > > optimal;
//         int l = 0;
//         int count = 0;
//         for(int r = 0; r < sheep.size(); r++){
//             if(sheep[r] == '*') count++;
//             if(r >= total){
//                 optimal.push_back(make_pair(count,make_pair(l,r)));
//                 if(sheep[l] == '*') count--;
//                 l++;
//             }
//         }
//         sort(optimal.rbegin(),optimal.rend());
//         // for(auto it : optimal){
//         //     cout << it.first << " " << it.second.first << " " << it.second.second << endl;
//         // }
//         int maxi = optimal[0].first;
//         if(maxi == 0){
//             cout << 0 << endl;
//             continue;
//         }
//         int ans = INT_MAX;
//         for(auto it : optimal){
//             if(it.first >= maxi - total){
//                 ans = min(ans,dist(sheep,it.second.first));
//             }
//             else break;
//         }
//         cout << ans << endl;
//     }
//     return 0;
// }
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        string sheep;
        cin >> sheep;

        vector<int> positions;
        for(int i = 0; i < n; i++){
            if(sheep[i] == '*') positions.push_back(i);
        }

        int m = positions.size();
        if(m == 0){
            cout << 0 << endl;
            continue;
        }

        int med = m / 2;
        int median = positions[med];

        long long moves = 0;
        for(int i = 0; i < m; i++){
            moves += abs(positions[i] - (median - med + i));
        }

        cout << moves << endl;
    }
    return 0;
}