Advertisement
erek1e

Fork - BIO 2023 Round 2

Apr 11th, 2023
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     freopen("input.txt", "r", stdin);
  7.     freopen("output.txt", "w", stdout);
  8.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  9.     int n, m, c; cin >> n >> m >> c;
  10.     vector<int> s(2*n), a(1+c);
  11.     for (int &x : s) cin >> x;
  12.     for (int i = 1; i <= c; ++i) cin >> a[i];
  13.     a[0] = s[0];
  14.  
  15.     vector<vector<int>> nextOccurance(1+m);
  16.     for (int i = c; i >= 0; --i) nextOccurance[a[i]].push_back(i);
  17.  
  18.     // Dijkstra's
  19.     vector<int> dist(2*n, c+1);
  20.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
  21.     dist[0] = 0, pq.emplace(0, 0);
  22.     vector<bool> seen(2*n);
  23.     int lastT = 0;
  24.     while (!pq.empty()) {
  25.         pair<int, int> pr = pq.top();
  26.         pq.pop();
  27.         int t = pr.first, v = pr.second;
  28.         if (seen[v]) continue;
  29.         seen[v] = true;
  30.  
  31.         while (lastT < t) {
  32.             nextOccurance[a[lastT]].pop_back();
  33.             ++lastT;
  34.         }
  35.  
  36.         vector<int> children;
  37.         if (v % n) children.push_back(v-1);
  38.         if ((v+1) % n) children.push_back(v+1);
  39.         children.push_back(v < n ? v+n : v-n);
  40.         for (int u : children) {
  41.             // find next point in time from t when the trendsetter changes to v[u]
  42.             /// currently brute force
  43.             int nextT = c+1;
  44.             if (!nextOccurance[s[u]].empty()) nextT = nextOccurance[s[u]].back();
  45.             if (nextT < dist[u]) {
  46.                 dist[u] = nextT;
  47.                 pq.emplace(nextT, u);
  48.             }
  49.         }
  50.     }
  51.  
  52.     int cnt = 0;
  53.     for (int i = 0; i < 2*n; ++i) cnt += dist[i] <= c || s[i] == a.back();
  54.     cout << cnt << endl;
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement