fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28. // 3 3
  29. // 2 8 7 4 1 3 5 6 9
  30. // 3 2 5
  31. // 1 4 6
  32. // 8 7 9
  33.  
  34.  
  35.  
  36.  
  37. vector<int> a;
  38. vector<vector<int>> grid;
  39.  
  40. int consistency(int m, int n){
  41.  
  42.  
  43. unordered_map<int,vector<int> > mp;
  44. for(int i=0; i<m; i++){
  45. for(int j=0; j<n; j++){
  46. mp[grid[i][j]] = {i,j};
  47. }
  48. }
  49.  
  50. vector<int> rows(m, n);
  51. vector<int> cols(n, m);
  52.  
  53. for(int i=0; i<a.size(); i++){
  54. int val = a[i];
  55. int x = mp[val][0];
  56. int y = mp[val][1];
  57.  
  58. rows[x]--;
  59. if(rows[x] == 0) return i;
  60.  
  61. cols[y]--;
  62. if(cols[y] == 0) return i;
  63. }
  64.  
  65. return -1;
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int practice(int n){
  83.  
  84.  
  85. return 0;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. void solve() {
  93.  
  94. int m, n;
  95. cin>> m >> n;
  96.  
  97. a.resize(m*n);
  98. for(int i=0; i<m*n; i++) cin >> a[i];
  99.  
  100. grid.resize(m, vector<int>(n));
  101. for(int i=0; i<m; i++){
  102. for(int j=0; j<n; j++){
  103. cin >> grid[i][j];
  104. }
  105. }
  106.  
  107. cout << consistency(m, n) << endl;
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int32_t main() {
  117. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  118.  
  119. int t = 1;
  120. // cin >> t;
  121. while (t--) {
  122. solve();
  123. }
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0s 5320KB
stdin
3 3
2 8 7 4 1 3 5 6 9 
3 2 5
1 4 6
8 7 9
stdout
3