fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b) {
  14. int x = 1;
  15. a %= M;
  16. while (b) {
  17. if (b & 1) x = (x * a) % M;
  18. a = (a * a) % M;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31.  
  32. //* 2 ptr
  33. int consistency1(int n){
  34.  
  35. int total = 0;
  36. for(int i=0; i<n; i++){
  37. if(a[i] == '*') total++;
  38. }
  39.  
  40. int mid = (total+1)/2;
  41.  
  42. int median = 0;
  43. int ct = 0;
  44. for(int i=0; i<n; i++){
  45. if(a[i] == '*') ct++;
  46. if(ct == mid){
  47. median = i;
  48. break;
  49. }
  50. }
  51.  
  52.  
  53. int ans = 0;
  54.  
  55. int empty = 0;
  56. for(int i=median-1; i>=0; i--){
  57. if(a[i] == '*'){
  58. ans += empty;
  59. }
  60. else empty++;
  61. }
  62. empty = 0;
  63. for(int i=median+1; i<n; i++){
  64. if(a[i] == '*') ans += empty;
  65. else empty++;
  66. }
  67.  
  68.  
  69. return ans;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. //* Optimized prefix
  77. int consistency2(int n){
  78.  
  79. vector<int> sheeps;
  80. int empty = 0;
  81. for(int i=0; i<n; i++){
  82. if(a[i] == '.') empty++;
  83. else sheeps.push_back(empty);
  84. }
  85.  
  86. int total = sheeps.size();
  87. int median = (total-1)/2;
  88.  
  89. int ans = 0;
  90. for(int i=0; i<total; i++){
  91. ans += abs(sheeps[i]-sheeps[median]);
  92. }
  93.  
  94.  
  95. return ans;
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. int practice(int n){
  113.  
  114.  
  115. return 0;
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. void solve() {
  123.  
  124. int n;
  125. cin>> n;
  126.  
  127. cin >> a;
  128.  
  129. cout << consistency1(n) << endl;
  130. // cout << consistency1(n) << " " << consistency2(n) << endl;
  131.  
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139. int32_t main() {
  140. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  141.  
  142. int t = 1;
  143. cin >> t;
  144. while (t--) {
  145. solve();
  146. }
  147.  
  148. return 0;
  149. }
Success #stdin #stdout 0s 5324KB
stdin
6
6
**.*..
5
*****
3
.*.
3
...
10
*.*...*.**
9
*.*...*.*
stdout
1
0
0
0
9
8