Advertisement
fireLUFFY

Codeforces-1520E

Sep 2nd, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #pragma GCC optimize("Ofast")
  2. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
  3. #pragma GCC optimize("unroll-loops")
  4. #pragma comment(linker, "/stack:200000000")
  5.  
  6. #include<bits/stdc++.h>
  7.  
  8. #define int long long
  9. #define ull unsigned long long
  10. #define vi vector<int>
  11. #define vvl vector<vector<int>>
  12. #define deql deque<int>
  13. #define prquel priority_queue<int>
  14. #define loop(i,n) for(int i=0;i<n;i++)
  15. #define loopn(i,a,b) for(int i=a;i<=b;i++)
  16. #define rloop(i,n) for(int i=(n-1);i>=0;i--)
  17. #define sortv(a) sort(a.begin(),a.end())
  18. #define sortvr(a) sort(a.rbegin(),a.rend())
  19. #define verase(a) a.erase(unique(a.begin(),a.end()),a.end())
  20. #define all(a) a.begin(),a.end()
  21. #define dbg(x) cout<<#x<<" = "<<x<<"\n";
  22. #define mp make_pair
  23. #define pb push_back
  24. #define pf push_front
  25. #define fi first
  26. #define se second
  27. #define endl "\n"
  28. #define inf 2e18
  29. using namespace std;
  30.  
  31. int MOD=1000000007;
  32. int mod=998244353;
  33. const int N=200050;
  34. const int nn=1000050;
  35. int fact[N]; // array to store values of factorial
  36. bool prime[nn]; //array to store precalculated primes till 10^6
  37. bool pow2(int x){if(x&&(!(x&(x-1)))) return true; else return false;}
  38. template<class T> void _print(vector<T> v1){ cerr<<"[ "; for(T i:v1){_print(i);cerr<<" ";}cerr<<"]";}
  39. template<class T> void _print(set<T> s1){ cerr<<"[ "; for(T i:s1){_print(i);cerr<<" ";}cerr<<"]";}
  40. template<class T> void _print(map<T,T> m1){for(auto i:m1){cerr<<"[ "; _print(i.first);cerr<<" : ";_print(i.second);cerr<<" ]";cerr<<endl;}}
  41. typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  42. template<class T> T gcd(T a,T b){if(!a||!b)return a|b; unsigned shift=__builtin_ctz(a|b); a>>=__builtin_ctz(a); do{b>>=__builtin_ctz(b); if(a>b)swap(a,b);b-=a;}while(b); return a<<shift;}
  43. template<class T> T lcm(T a,T b){ unsigned val=max(a,b)/gcd(a,b);val*=min(a,b);return val;}
  44. int multiply(int x, int y){return (x * 1ll * y) % MOD;}
  45. int power(int x, int y){int z = 1;while(y){if(y & 1) z = multiply(z, x);x = multiply(x, x);y >>= 1;}return z;}
  46. int modInverse(int x){return power(x, MOD - 2);}
  47. int divide(int x, int y){return multiply(x, modInverse(y));}
  48. void cal_factorial(){fact[0] = 1;for(int i = 1; i < N; i++)fact[i] = multiply(fact[i - 1], i);}// function to calculate factorial upto N
  49. void cal_primes(){memset(prime,true,sizeof(prime)); loopn(i,2,sqrt(nn)){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  50. int nCr(int n, int k){return divide(fact[n], multiply(fact[k], fact[n - k]));}
  51. vector<int>Intersection(vi &v1,vi &v2){vi v3;sort(all(v1));sort(all(v2));set_intersection(all(v1),all(v2),back_inserter(v3));return v3;}
  52. vector<int>Union(vi &v1,vi&v2){vi v3;sort(all(v1));sort(all(v2));set_union(all(v1),all(v2),back_inserter(v3));return v3;}
  53. void FASTIO(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
  54. int dx[8]={0,1,0,-1,1,1,-1,-1}; //direction vectors
  55. int dy[8]={1,0,-1,0,1,-1,-1,1}; //direction vectors
  56.  
  57.  
  58. void solve(int t)
  59. {
  60.     int testcases=t;
  61.     while(t--)
  62.     {
  63.         //cout<<"#"<<(testcases-t)<<" Test-Case: "<<endl;
  64.         int n;cin>>n;
  65.         string s;
  66.         int sc=0;
  67.         cin>>s;
  68.         loop(i,n)
  69.         if(s[i]=='*')
  70.             sc++;
  71.  
  72.         vi l(n,0),r(n,0);
  73.         int cnt=0;
  74.         loop(i,n)
  75.         {
  76.             if(s[i]=='*')
  77.             {
  78.                 cnt++;
  79.                 if(i!=0)
  80.                     r[i]=r[i-1];
  81.             }
  82.             else
  83.             {
  84.                 if(i!=0)
  85.                     r[i]=(r[i-1]+cnt);
  86.             }
  87.         }
  88.  
  89.         cnt=0;
  90.         rloop(i,n)
  91.         {
  92.             if(s[i]=='*')
  93.             {
  94.                 cnt++;
  95.                 if(i!=(n-1))
  96.                     l[i]=l[i+1];
  97.             }
  98.             else
  99.             {
  100.                 if(i!=(n-1))
  101.                     l[i]=(l[i+1]+cnt);
  102.             }
  103.         }
  104.  
  105.         if(sc<2)
  106.             cout<<0<<endl;
  107.         else
  108.         {
  109.             int ans=10000000000000000;
  110.             loop(i,n)
  111.             {
  112.                 ans=min(ans,l[i+1]+r[i]);
  113.             }
  114.  
  115.             cout<<ans<<endl;
  116.         }
  117.     }
  118. }
  119.  
  120. main()
  121. {
  122.     auto start=chrono::system_clock::now();
  123.     {
  124.         #ifndef ONLINE_JUDGE
  125.             freopen("input.txt","r",stdin);
  126.             freopen("output.txt","w",stdout);
  127.         #endif
  128.         FASTIO();  
  129.         int t=1;
  130.         cin>>t;
  131.         solve(t);
  132.     }
  133.     auto end=chrono::system_clock::now();
  134.     chrono::duration<double> elapsed=end-start;
  135. //  cout<<" Time taken: "<<elapsed.count()<<" sec";
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement