fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define read(type) readInt<type>() // Fast read
  4. #define ll long long
  5. #define nL "\n"
  6. #define pb push_back
  7. #define mk make_pair
  8. #define pii pair<int, int>
  9. #define a first
  10. #define b second
  11. #define vi vector<int>
  12. #define all(x) (x).begin(), (x).end()
  13. #define umap unordered_map
  14. #define uset unordered_set
  15. #define MOD 1000000007
  16. #define imax INT_MAX
  17. #define imin INT_MIN
  18. #define exp 1e9
  19. #define sz(x) (int((x).size()))
  20.  
  21.  
  22. void solve() {
  23. int n; cin >> n;
  24.  
  25. int ht = 0; int gt = 0;
  26. vector<char> HG(n); for(auto i = 0; i < n; i++) {
  27. char d; cin >> d;
  28. if (d == 'H') {ht++;}
  29. else {gt++;}
  30.  
  31. HG[i] = d;
  32. }
  33.  
  34. set<int> H; set<int> G;
  35.  
  36. for(auto i = 0; i < n; i++) {
  37. int x; cin >> x;
  38.  
  39. int hc = 0; int gc = 0;
  40.  
  41. if (HG[i] == 'H') {
  42. cout << "Processing H" << endl;
  43. vector<int> gPos;
  44. for(auto j = i; j < x; j++) {
  45. if (HG[j] == 'H') {hc++;}
  46. else {gPos.pb(j); gc++;}
  47. }
  48.  
  49. cout << "H count: " << hc << endl;
  50. cout << "gPos size: " << (int)gPos.size() << endl;
  51.  
  52. if (hc==ht) {H.insert(i);}
  53. if ((int)gPos.size() > 0) {
  54. for(auto v : gPos) {G.insert(v);}
  55. H.insert(i);
  56. }
  57.  
  58.  
  59. if (hc!=ht && (int)gPos.size() == 0) {H.erase(i);}
  60. } else {
  61. cout << "Processing G" << endl;
  62. vector<int> hPos;
  63. for(auto j = i; j < x; j++) {
  64. if (HG[j] == 'G') {gc++;}
  65. else {hPos.pb(j); hc++;}
  66. }
  67.  
  68. cout << "G count: " << gc << endl;
  69. cout << "hPos size: " << (int)hPos.size() << endl;
  70.  
  71. if (gc==gt) {G.insert(i);}
  72. if ((int)hPos.size() > 0) {
  73. for(auto v : hPos) {H.insert(v);}
  74. G.insert(i);
  75. }
  76.  
  77.  
  78. if(gc!=gt && (int)hPos.size() == 0) {G.erase(i);}
  79. }
  80. }
  81.  
  82. cout << "H size " << (int)H.size() << endl;
  83. cout << "G size " << (int)G.size() << endl;
  84.  
  85. cout << (int)H.size()*(int)G.size() << endl;
  86. }
  87.  
  88.  
  89.  
  90. int32_t main()
  91. {
  92. ios_base::sync_with_stdio(false);
  93. cin.tie(NULL);
  94. solve();
  95. return 0;
  96. }
Success #stdin #stdout 0.01s 5412KB
stdin
4
GHHG
2 4 3 4
stdout
Processing G
G count: 1
hPos size: 1
Processing H
H count: 2
gPos size: 1
Processing H
H count: 1
gPos size: 0
Processing G
G count: 1
hPos size: 0
H size 1
G size 1
1