Advertisement
Salman_CUET_18

RGB Triplets

Apr 12th, 2020
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. // In The Name of Almighty Allah
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define             fastio              ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5. #define             ff                  first
  6. #define             ss                  second
  7. #define             int                 long long
  8. #define             pb                  push_back
  9. #define             pf                  push_front
  10. #define             pii                 pair<int,int>
  11. #define             print(x)            for(auto it : x) cout<<it<<' '; cout<<'\n';
  12. #define             debug(x)            cout << #x << " --->> " << x << endl;
  13. #define             mod                 1000000007
  14. #define             inf                 1e18
  15. #define             PI                  (acos(-1))
  16. #define             eps                 (1e-8)
  17. #define             clear(x)            memset(x,0,sizeof(x));
  18.  
  19.  
  20. main()
  21. {
  22.     fastio;
  23. #ifndef ONLINE_JUDGE
  24.     freopen("input.txt", "r", stdin);
  25.     freopen("output.txt", "w", stdout);
  26. #endif
  27.  
  28.     int n;
  29.  
  30.     string s;
  31.  
  32.     cin >> n;
  33.     cin >> s;
  34.  
  35.     pair<int, pii>cum[n];
  36.  
  37.     int ans = 0, r = 0, g = 0, b = 0;
  38.  
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         if (s[i] == 'R') r++;
  42.         else if (s[i] == 'B') b++;
  43.         else g++;
  44.  
  45.         cum[i].ff = r, cum[i].ss.ff = g, cum[i].ss.ss = b;
  46.     }
  47.  
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         for (int j = i + 1; j < n - 1; j++)
  51.         {
  52.             if (s[j] == s[i]) continue;
  53.  
  54.             int k = 2 * j - i;
  55.             // debug(k);
  56.             if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R'))
  57.             {
  58.                 ans += (cum[n - 1].ss.ss - cum[j].ss.ss);
  59.  
  60.                 if (k < n && s[k] == 'B')
  61.                     ans--;
  62.             }
  63.             else if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R'))
  64.             {
  65.                 ans += (cum[n - 1].ss.ff - cum[j].ss.ff);
  66.  
  67.                 if (k < n && s[k] == 'G')
  68.                     ans--;
  69.             }
  70.             else if ((s[i] == 'G' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'G'))
  71.             {
  72.                 ans += (cum[n - 1].ff - cum[j].ff);
  73.  
  74.                 if (k < n && s[k] == 'R')
  75.                     ans--;
  76.             }
  77.         }
  78.     }
  79.     cout << ans << endl;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement