fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. void consistency(string s) {
  27.  
  28. int n = s.size();
  29.  
  30. int l=0, lc=0, lct=0, ct = 0, c=0;
  31. for(int i=0; i<n; i++){
  32. if(s[i] == 'L') l++;
  33. else if(s[i] == 'C'){
  34. lc += l;
  35. c++;
  36. }
  37. else if(s[i] == 'T'){
  38. lct += lc;
  39. ct += c;
  40. }
  41. }
  42.  
  43. int llct = lct + lc;
  44. int lctt = lct + ct;
  45.  
  46. vector<int> prefixL(n+1, 0), suffixT(n+1, 0);
  47. for(int i=0; i<n; i++){
  48. prefixL[i+1] = prefixL[i];
  49. if(s[i] == 'L') prefixL[i+1]++;
  50. }
  51. for(int i=n-1; i>=0; i--){
  52. suffixT[i] = suffixT[i+1];
  53. if(s[i] == 'T') suffixT[i]++;
  54. }
  55.  
  56. int lcct = 0;
  57. for(int i=1; i<n-1; i++){
  58. lcct = max(lcct, prefixL[i] * suffixT[i]);
  59. }
  60. lcct += lct;
  61.  
  62. int ans = max(lct, lctt);
  63. ans = max(ans, llct);
  64. ans = max(ans, lcct);
  65.  
  66. cout << ans << endl;
  67.  
  68. }
  69.  
  70. void solve() {
  71.  
  72. string s = "LMCT";
  73. consistency(s) ;
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int32_t main() {
  82. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  83.  
  84. int t = 1;
  85. while (t--) {
  86. solve();
  87. }
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
2