Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- using namespace std;
- const int maxn = 305;
- int mat[maxn][maxn];
- struct rudnik {
- int godina, x, y, at, odgovor;
- rudnik() {}
- rudnik(int _godina, int _x, int _y, int _at, int _odgovor) {
- godina = _godina;
- x = _x;
- y = _y;
- at = _at;
- odgovor = _odgovor;
- }
- bool operator < (const rudnik & tmp) const {
- if(godina == tmp.godina) {
- return odgovor < tmp.odgovor;
- }
- return godina < tmp.godina;
- }
- };
- const int MAXN = 1e5 + 10;
- int idx[MAXN], sz[MAXN];
- void init() {
- for(int i = 0; i < MAXN; i++) {
- sz[i] = 1;
- idx[i] = i;
- }
- }
- int find_root(int x) {
- while(x != idx[x]) {
- idx[x] = idx[idx[x]];
- x = idx[x];
- }
- return x;
- }
- void unite(int A, int B) {
- int rootA = find_root(A);
- int rootB = find_root(B);
- if(rootA != rootB) {
- if(sz[rootA] < sz[rootB]) {
- idx[rootA] = idx[rootB];
- sz[rootB] += sz[rootA];
- }
- else {
- idx[rootB] = idx[rootA];
- sz[rootA] += sz[rootB];
- }
- }
- }
- bool check(int A, int B) {
- return find_root(A) == find_root(B);
- }
- int main() {
- vector<rudnik> rudnici;
- init();
- int k;
- cin >> k;
- vector<int> godini(k);
- for(int i = 0; i < k; i++) {
- cin >> godini[i];
- rudnici.push_back(rudnik(godini[i], -1, -1, i, 1));
- }
- int n, m;
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- rudnici.push_back(rudnik(mat[i][j], i, j, -1, -1));
- }
- }
- sort(rudnici.begin(), rudnici.end());
- vector<int> res(k + 1);
- int oblasti = 0;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, 1, -1};
- vector<vector<bool>> visited(n, vector<bool>(m, false));
- for(int i = (int) rudnici.size() - 1; i >= 0; i--) {
- if(rudnici[i].odgovor == 1) {
- res[rudnici[i].at] = oblasti;
- }
- else {
- int ci = rudnici[i].x;
- int cj = rudnici[i].y;
- visited[ci][cj] = true;
- oblasti++;
- for(int l = 0; l < 4; l++) {
- int ti = ci + di[l];
- int tj = cj + dj[l];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and visited[ti][tj]) {
- int A = ti * m + tj;
- int B = ci * m + cj;
- if(!check(A, B)) {
- oblasti--;
- }
- unite(A, B);
- }
- }
- }
- }
- for(int i = 0; i < k; i++) {
- cout <<res[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment