Josif_tepe

Untitled

Jan 17th, 2023
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. #include <set>
  6. #include <cstring>
  7. using namespace std;
  8.  
  9. struct node {
  10.     int year, idx, x, y;
  11.     bool ok;
  12.     node () {}
  13.     node(int _year, int _idx, int _x, int _y, bool _ok) {
  14.         year = _year;
  15.         idx = _idx;
  16.         x = _x;
  17.         y = _y;
  18.         ok = _ok;
  19.     }
  20.     bool operator < (const node &tmp) const {
  21.         if(year == tmp.year) {
  22.             return ok > tmp.ok;
  23.         }
  24.         return year < tmp.year;
  25.     }
  26. };
  27. const int maxn = 1e5 + 10;
  28. int mat[333][333];
  29. bool exploited[333][333];
  30. int root[maxn];
  31. int sz[maxn];
  32. void init() {
  33.     for(int i = 0; i < maxn; i++) {
  34.         root[i] = i;
  35.         sz[i] = 1;
  36.     }
  37. }
  38.  
  39. int find_root(int x) {
  40.     while(root[x] != x) {
  41.         root[x] = root[root[x]];
  42.         x = root[x];
  43.     }
  44.     return x;
  45. }
  46. void unite(int a, int b) {
  47.     int root_a = find_root(a);
  48.     int root_b = find_root(b);
  49.     if(root_a != root_b) {
  50.         if(sz[root_a] < sz[root_b]) {
  51.             sz[root_b] += sz[root_a];
  52.             root[root_a] = root[root_b];
  53.         }
  54.         else {
  55.             sz[root_a] += sz[root_b];
  56.             root[root_b] = root[root_a];
  57.         }
  58.     }
  59. }
  60. int main()
  61. {
  62.     init();
  63.     int n;
  64.     cin >> n;
  65.     vector<int> years(n);
  66.     vector<node> v;
  67.     for(int i = 0; i < n; i++) {
  68.         cin >> years[i];
  69.         v.push_back(node(years[i], i, -1, -1, false));
  70.     }
  71.     vector<int> result(n);
  72.     int r, c;
  73.     cin >> r >> c;
  74.     for(int i = 0; i < r; i++) {
  75.         for(int j = 0; j < c; j++) {
  76.             cin >> mat[i][j];
  77.             v.push_back(node(mat[i][j], -1, i, j, true));
  78.         }
  79.     }
  80.     memset(exploited, false, sizeof exploited);
  81.     sort(v.begin(), v.end());
  82.     int oblasti = 0;
  83.    
  84.     int di[] = {-1, 1, 0, 0};
  85.     int dj[] = {0, 0, -1, 1};
  86.    
  87.     for(int i = (int) v.size() - 1; i >= 0; i--) {
  88.         if(v[i].ok == false) {
  89.             result[v[i].idx] = oblasti;
  90.         }
  91.         else {
  92.             int pos_i = v[i].x;
  93.             int pos_j = v[i].y;
  94.             exploited[pos_i][pos_j] = true;
  95.             oblasti++;
  96.             for(int k = 0; k < 4; k++){
  97.                 int ti = pos_i + di[k];
  98.                 int tj = pos_j + dj[k];
  99.                 if(ti >= 0 and tj >= 0 and ti < r and tj < c and exploited[ti][tj] == true) {
  100.                     int A = pos_i * c + pos_j;
  101.                     int B = ti * c + tj;
  102.                     if(find_root(A) != find_root(B)) {
  103.                         oblasti--;
  104.                     }
  105.                     unite(A, B);
  106.                 }
  107.             }
  108.            
  109.         }
  110.     }
  111.     for(int i = 0; i < n; i++) {
  112.         cout << result[i] << endl;
  113.     }
  114.     return 0;
  115. }
  116. /*
  117.  2013 2015 2023 2018 2023 2019
  118.  2013 2023 2015 2013 2021 2025
  119.  2015 2013 2022 2016 2015 2015
  120.  2013 2015 2020 2020 2015 2013
  121.  
  122.  **/
  123.  
Add Comment
Please, Sign In to add comment