fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using ld = long double;
  4. #define int long long
  5. #define endl '\n'
  6. #define all(x) x.begin(), x.end()
  7.  
  8. const int mod = 1e9 + 7;
  9.  
  10.  
  11. void solve() {
  12. int n; cin >> n;
  13. array<vector<string>, 5> mpch;
  14. map<string, int> nonchamp;
  15. vector<pair<string,int>> champ;
  16. map<string, bool> ch;
  17. for(int i = 0; i < 5*n; i++) {
  18. string s; cin >> s;
  19. ch[s] = true;
  20. }
  21. int m; cin >> m;
  22.  
  23. int noncnt = 0, cnt = 0;
  24. for(int i = 0; i < m; i++) {
  25. string s; cin >> s;
  26. int pos; cin >> pos;
  27. if(ch.find(s) != ch.end()) {
  28. cnt++;
  29. mpch[pos-1].push_back(s);
  30. } else {
  31. noncnt++; nonchamp[s] = pos-1;
  32. }
  33. // cout << s << ' ' << pos << '\n';
  34. }
  35. int mx= 0;
  36. for(int i = 0; i < 5; i++) mx = max(mx, (int)mpch[i].size());
  37. for(int i = 0; i < mx; i++) {
  38. for(int j = 0; j < 5; j++) {
  39. // cout << j << ' ' << i << '\n';
  40. if(mpch[j].size() <= i) continue;
  41. champ.push_back({mpch[j][i], j});
  42. }
  43. }
  44. vector<array<int, 5>> teams(cnt);
  45. vector<int> rep(cnt);
  46. vector<bool> isteam(cnt);
  47.  
  48. int ind = 0;
  49. for(auto& [s, y] : champ) {
  50. teams[ind][y] = true;
  51. rep[ind] = y;
  52. ind++;
  53. }
  54.  
  55. for(int i = 0; i < cnt; i++) {
  56. if(noncnt >= 4) {
  57. noncnt -= 4;
  58. isteam[i] = true;
  59. } else {
  60. isteam[i] = false;
  61. for(int j = i-1; j >= 0; j--) {
  62. if(isteam[j] == false) continue;
  63. else {
  64. if(teams[j][rep[i]] == false) {
  65. teams[j][rep[i]] = true;
  66. noncnt++;
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. int ans = 0;
  74. for(int i = 0; i < champ.size(); i++) {
  75. if(isteam[i]) ans++;
  76. }
  77. cout << ans << '\n';
  78.  
  79. }
  80.  
  81. signed main() {
  82. ios_base::sync_with_stdio(0); cin.tie(nullptr);
  83. int tc = 1;
  84. // cin >> tc;
  85. for(int i = 1; i <= tc; i++) {
  86. solve();
  87. }
  88. }
Success #stdin #stdout 0.01s 584KB
stdin
Standard input is empty
stdout
0