Submission #1032253

# Submission time Handle Problem Language Result Execution time Memory
1032253 2024-07-23T14:03:04 Z Tonyl Swapping Cities (APIO20_swap) C++17
100 / 100
744 ms 80372 KB
#include "swap.h" //"grader.cpp"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pi = pair<int,int>;
using ll = long long;
#define REP(i,n) for (int i = 0; i < n; i++)
#define trav(a,x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define D(x) cerr << #x << ": " << x << endl;

int n, m;
vi weights;
vector<vector<pi>> w_to_edges;

struct History {
  int curr = -1;
  map<int, int> time_to_val;

  History() {
    time_to_val = {};
    update(0, 0);
  }

  History(int val) {
    time_to_val = {};
    update(0, val);
  }

  void update(int time, int val) {
    if (val == curr) return;
    time_to_val[time] = val;
    curr = val;
  }

  int at_time(int time) {
    auto iter = time_to_val.upper_bound(time);
    iter--;
    return (*iter).second;
  }
};


const int MAX_N = 100001;
int sz[MAX_N];
History parent[MAX_N], flag[MAX_N], neicount[MAX_N];

int root(int a) {
  if (parent[a].curr != a) return root(parent[a].curr);
  else return a;
}

int root_at(int tm, int a) {
  int par = parent[a].at_time(tm);
  if (par != a) return root_at(tm, par);
  else return a;
}

void join(int tm, int u, int v) {
  int a = root(u);
  int b = root(v);

  neicount[u].update(tm, neicount[u].curr+1);
  neicount[v].update(tm, neicount[v].curr+1);

  if (neicount[u].curr == 3 || neicount[v].curr == 3) flag[a].update(tm, 1);
  if (a == b) {
    flag[a].update(tm, 1); // cycle
    return;
  }

  if (sz[a] < sz[b]) swap(a,b);
  parent[b].update(tm, a);
  sz[a] += sz[b];
  if (flag[b].curr) flag[a].update(tm, 1);
}

bool were_good(int tm, int u, int v) {
  bool yay = false; //neicount[u].at_time(tm)>=2 || neicount[v].at_time(tm)>=2;
  u = root_at(tm, u); v = root_at(tm, v);
  yay = yay || flag[u].at_time(tm);
  return u == v && yay;
}

void go_insert() {
  REP(tm, weights.size()) {
    trav(e, w_to_edges[tm]) {
      join(tm+1, e.first, e.second);
    }
  }
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  n = N; m = M;
  vector<pi> wp(m);
  REP(i,m) wp[i] = {W[i], i};
  sort(all(wp));
  int ind = -1, prev = -1;
  REP(i, m) {
    if (wp[i].first != prev) {
      prev = wp[i].first; ind++;
      weights.push_back(prev);
      w_to_edges.push_back(vector<pi>());
    }
    int j = wp[i].second;
    w_to_edges[ind].push_back({U[j], V[j]});
  }

  REP(i,n) parent[i] = History(i);
  REP(i,n) sz[i] = 1;

  go_insert();
}

int getMinimumFuelCapacity(int X, int Y) {

  int l = 0, r = weights.size()+1;
  while (l < r) {
    int mid = (l+r)/2;
    if (were_good(mid, X, Y)) {
      r = mid;
    } else {
      l = mid+1;
    }
  }

  assert(l==r);
  assert(l!=0);

  if (l > weights.size()) return -1;
  else return weights[l-1];
}

Compilation message

swap.cpp: In function 'void go_insert()':
swap.cpp:7:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define REP(i,n) for (int i = 0; i < n; i++)
......
   86 |   REP(tm, weights.size()) {
      |       ~~~~~~~~~~~~~~~~~~            
swap.cpp:86:3: note: in expansion of macro 'REP'
   86 |   REP(tm, weights.size()) {
      |   ^~~
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:130:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  130 |   if (l > weights.size()) return -1;
      |       ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 30808 KB Output is correct
2 Correct 22 ms 30812 KB Output is correct
3 Correct 21 ms 30800 KB Output is correct
4 Correct 22 ms 31068 KB Output is correct
5 Correct 24 ms 31004 KB Output is correct
6 Correct 23 ms 31068 KB Output is correct
7 Correct 22 ms 31068 KB Output is correct
8 Correct 21 ms 30972 KB Output is correct
9 Correct 120 ms 50988 KB Output is correct
10 Correct 139 ms 55548 KB Output is correct
11 Correct 140 ms 55040 KB Output is correct
12 Correct 169 ms 56316 KB Output is correct
13 Correct 131 ms 56324 KB Output is correct
14 Correct 148 ms 51252 KB Output is correct
15 Correct 480 ms 59524 KB Output is correct
16 Correct 520 ms 58884 KB Output is correct
17 Correct 497 ms 60412 KB Output is correct
18 Correct 278 ms 60240 KB Output is correct
19 Correct 199 ms 40060 KB Output is correct
20 Correct 483 ms 60104 KB Output is correct
21 Correct 468 ms 59620 KB Output is correct
22 Correct 530 ms 61040 KB Output is correct
23 Correct 354 ms 61272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 30808 KB Output is correct
2 Correct 22 ms 30812 KB Output is correct
3 Correct 279 ms 59060 KB Output is correct
4 Correct 305 ms 60312 KB Output is correct
5 Correct 302 ms 59804 KB Output is correct
6 Correct 305 ms 60068 KB Output is correct
7 Correct 275 ms 60188 KB Output is correct
8 Correct 301 ms 59160 KB Output is correct
9 Correct 277 ms 59772 KB Output is correct
10 Correct 263 ms 58784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 30808 KB Output is correct
2 Correct 22 ms 30812 KB Output is correct
3 Correct 21 ms 30800 KB Output is correct
4 Correct 22 ms 31068 KB Output is correct
5 Correct 24 ms 31004 KB Output is correct
6 Correct 23 ms 31068 KB Output is correct
7 Correct 22 ms 31068 KB Output is correct
8 Correct 21 ms 30972 KB Output is correct
9 Correct 20 ms 30932 KB Output is correct
10 Correct 24 ms 31068 KB Output is correct
11 Correct 22 ms 31216 KB Output is correct
12 Correct 22 ms 31068 KB Output is correct
13 Correct 22 ms 31012 KB Output is correct
14 Correct 22 ms 31184 KB Output is correct
15 Correct 22 ms 31064 KB Output is correct
16 Correct 33 ms 31068 KB Output is correct
17 Correct 23 ms 30992 KB Output is correct
18 Correct 21 ms 31024 KB Output is correct
19 Correct 21 ms 31068 KB Output is correct
20 Correct 23 ms 31068 KB Output is correct
21 Correct 21 ms 31060 KB Output is correct
22 Correct 22 ms 31320 KB Output is correct
23 Correct 21 ms 30988 KB Output is correct
24 Correct 22 ms 31316 KB Output is correct
25 Correct 29 ms 31320 KB Output is correct
26 Correct 32 ms 31308 KB Output is correct
27 Correct 27 ms 31208 KB Output is correct
28 Correct 22 ms 31324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 30932 KB Output is correct
2 Correct 22 ms 30808 KB Output is correct
3 Correct 22 ms 30812 KB Output is correct
4 Correct 21 ms 30800 KB Output is correct
5 Correct 22 ms 31068 KB Output is correct
6 Correct 24 ms 31004 KB Output is correct
7 Correct 23 ms 31068 KB Output is correct
8 Correct 22 ms 31068 KB Output is correct
9 Correct 21 ms 30972 KB Output is correct
10 Correct 120 ms 50988 KB Output is correct
11 Correct 139 ms 55548 KB Output is correct
12 Correct 140 ms 55040 KB Output is correct
13 Correct 169 ms 56316 KB Output is correct
14 Correct 131 ms 56324 KB Output is correct
15 Correct 24 ms 31068 KB Output is correct
16 Correct 22 ms 31216 KB Output is correct
17 Correct 22 ms 31068 KB Output is correct
18 Correct 22 ms 31012 KB Output is correct
19 Correct 22 ms 31184 KB Output is correct
20 Correct 22 ms 31064 KB Output is correct
21 Correct 33 ms 31068 KB Output is correct
22 Correct 23 ms 30992 KB Output is correct
23 Correct 21 ms 31024 KB Output is correct
24 Correct 21 ms 31068 KB Output is correct
25 Correct 23 ms 31068 KB Output is correct
26 Correct 21 ms 31060 KB Output is correct
27 Correct 22 ms 31320 KB Output is correct
28 Correct 21 ms 30988 KB Output is correct
29 Correct 22 ms 31316 KB Output is correct
30 Correct 29 ms 31320 KB Output is correct
31 Correct 32 ms 31308 KB Output is correct
32 Correct 27 ms 31208 KB Output is correct
33 Correct 22 ms 31324 KB Output is correct
34 Correct 34 ms 34456 KB Output is correct
35 Correct 142 ms 55872 KB Output is correct
36 Correct 179 ms 55816 KB Output is correct
37 Correct 160 ms 55744 KB Output is correct
38 Correct 154 ms 55548 KB Output is correct
39 Correct 153 ms 55548 KB Output is correct
40 Correct 161 ms 53760 KB Output is correct
41 Correct 169 ms 56328 KB Output is correct
42 Correct 155 ms 55812 KB Output is correct
43 Correct 132 ms 55856 KB Output is correct
44 Correct 166 ms 57460 KB Output is correct
45 Correct 196 ms 65476 KB Output is correct
46 Correct 146 ms 55980 KB Output is correct
47 Correct 182 ms 55892 KB Output is correct
48 Correct 169 ms 57856 KB Output is correct
49 Correct 174 ms 62712 KB Output is correct
50 Correct 122 ms 55804 KB Output is correct
51 Correct 152 ms 61000 KB Output is correct
52 Correct 214 ms 69112 KB Output is correct
53 Correct 284 ms 74232 KB Output is correct
54 Correct 309 ms 76536 KB Output is correct
55 Correct 143 ms 55700 KB Output is correct
56 Correct 254 ms 71900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 30808 KB Output is correct
2 Correct 22 ms 30812 KB Output is correct
3 Correct 21 ms 30800 KB Output is correct
4 Correct 22 ms 31068 KB Output is correct
5 Correct 24 ms 31004 KB Output is correct
6 Correct 23 ms 31068 KB Output is correct
7 Correct 22 ms 31068 KB Output is correct
8 Correct 21 ms 30972 KB Output is correct
9 Correct 120 ms 50988 KB Output is correct
10 Correct 139 ms 55548 KB Output is correct
11 Correct 140 ms 55040 KB Output is correct
12 Correct 169 ms 56316 KB Output is correct
13 Correct 131 ms 56324 KB Output is correct
14 Correct 148 ms 51252 KB Output is correct
15 Correct 480 ms 59524 KB Output is correct
16 Correct 520 ms 58884 KB Output is correct
17 Correct 497 ms 60412 KB Output is correct
18 Correct 278 ms 60240 KB Output is correct
19 Correct 279 ms 59060 KB Output is correct
20 Correct 305 ms 60312 KB Output is correct
21 Correct 302 ms 59804 KB Output is correct
22 Correct 305 ms 60068 KB Output is correct
23 Correct 275 ms 60188 KB Output is correct
24 Correct 301 ms 59160 KB Output is correct
25 Correct 277 ms 59772 KB Output is correct
26 Correct 263 ms 58784 KB Output is correct
27 Correct 24 ms 31068 KB Output is correct
28 Correct 22 ms 31216 KB Output is correct
29 Correct 22 ms 31068 KB Output is correct
30 Correct 22 ms 31012 KB Output is correct
31 Correct 22 ms 31184 KB Output is correct
32 Correct 22 ms 31064 KB Output is correct
33 Correct 33 ms 31068 KB Output is correct
34 Correct 23 ms 30992 KB Output is correct
35 Correct 21 ms 31024 KB Output is correct
36 Correct 34 ms 34456 KB Output is correct
37 Correct 142 ms 55872 KB Output is correct
38 Correct 179 ms 55816 KB Output is correct
39 Correct 160 ms 55744 KB Output is correct
40 Correct 154 ms 55548 KB Output is correct
41 Correct 153 ms 55548 KB Output is correct
42 Correct 161 ms 53760 KB Output is correct
43 Correct 169 ms 56328 KB Output is correct
44 Correct 155 ms 55812 KB Output is correct
45 Correct 132 ms 55856 KB Output is correct
46 Correct 166 ms 57460 KB Output is correct
47 Correct 54 ms 34700 KB Output is correct
48 Correct 549 ms 59936 KB Output is correct
49 Correct 533 ms 60000 KB Output is correct
50 Correct 490 ms 59948 KB Output is correct
51 Correct 540 ms 59844 KB Output is correct
52 Correct 478 ms 58624 KB Output is correct
53 Correct 414 ms 53776 KB Output is correct
54 Correct 480 ms 61272 KB Output is correct
55 Correct 487 ms 59984 KB Output is correct
56 Correct 309 ms 59856 KB Output is correct
57 Correct 463 ms 62036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 30932 KB Output is correct
2 Correct 22 ms 30808 KB Output is correct
3 Correct 22 ms 30812 KB Output is correct
4 Correct 21 ms 30800 KB Output is correct
5 Correct 22 ms 31068 KB Output is correct
6 Correct 24 ms 31004 KB Output is correct
7 Correct 23 ms 31068 KB Output is correct
8 Correct 22 ms 31068 KB Output is correct
9 Correct 21 ms 30972 KB Output is correct
10 Correct 120 ms 50988 KB Output is correct
11 Correct 139 ms 55548 KB Output is correct
12 Correct 140 ms 55040 KB Output is correct
13 Correct 169 ms 56316 KB Output is correct
14 Correct 131 ms 56324 KB Output is correct
15 Correct 148 ms 51252 KB Output is correct
16 Correct 480 ms 59524 KB Output is correct
17 Correct 520 ms 58884 KB Output is correct
18 Correct 497 ms 60412 KB Output is correct
19 Correct 278 ms 60240 KB Output is correct
20 Correct 279 ms 59060 KB Output is correct
21 Correct 305 ms 60312 KB Output is correct
22 Correct 302 ms 59804 KB Output is correct
23 Correct 305 ms 60068 KB Output is correct
24 Correct 275 ms 60188 KB Output is correct
25 Correct 301 ms 59160 KB Output is correct
26 Correct 277 ms 59772 KB Output is correct
27 Correct 263 ms 58784 KB Output is correct
28 Correct 24 ms 31068 KB Output is correct
29 Correct 22 ms 31216 KB Output is correct
30 Correct 22 ms 31068 KB Output is correct
31 Correct 22 ms 31012 KB Output is correct
32 Correct 22 ms 31184 KB Output is correct
33 Correct 22 ms 31064 KB Output is correct
34 Correct 33 ms 31068 KB Output is correct
35 Correct 23 ms 30992 KB Output is correct
36 Correct 21 ms 31024 KB Output is correct
37 Correct 34 ms 34456 KB Output is correct
38 Correct 142 ms 55872 KB Output is correct
39 Correct 179 ms 55816 KB Output is correct
40 Correct 160 ms 55744 KB Output is correct
41 Correct 154 ms 55548 KB Output is correct
42 Correct 153 ms 55548 KB Output is correct
43 Correct 161 ms 53760 KB Output is correct
44 Correct 169 ms 56328 KB Output is correct
45 Correct 155 ms 55812 KB Output is correct
46 Correct 132 ms 55856 KB Output is correct
47 Correct 166 ms 57460 KB Output is correct
48 Correct 54 ms 34700 KB Output is correct
49 Correct 549 ms 59936 KB Output is correct
50 Correct 533 ms 60000 KB Output is correct
51 Correct 490 ms 59948 KB Output is correct
52 Correct 540 ms 59844 KB Output is correct
53 Correct 478 ms 58624 KB Output is correct
54 Correct 414 ms 53776 KB Output is correct
55 Correct 480 ms 61272 KB Output is correct
56 Correct 487 ms 59984 KB Output is correct
57 Correct 309 ms 59856 KB Output is correct
58 Correct 463 ms 62036 KB Output is correct
59 Correct 199 ms 40060 KB Output is correct
60 Correct 483 ms 60104 KB Output is correct
61 Correct 468 ms 59620 KB Output is correct
62 Correct 530 ms 61040 KB Output is correct
63 Correct 354 ms 61272 KB Output is correct
64 Correct 21 ms 31068 KB Output is correct
65 Correct 23 ms 31068 KB Output is correct
66 Correct 21 ms 31060 KB Output is correct
67 Correct 22 ms 31320 KB Output is correct
68 Correct 21 ms 30988 KB Output is correct
69 Correct 22 ms 31316 KB Output is correct
70 Correct 29 ms 31320 KB Output is correct
71 Correct 32 ms 31308 KB Output is correct
72 Correct 27 ms 31208 KB Output is correct
73 Correct 22 ms 31324 KB Output is correct
74 Correct 196 ms 65476 KB Output is correct
75 Correct 146 ms 55980 KB Output is correct
76 Correct 182 ms 55892 KB Output is correct
77 Correct 169 ms 57856 KB Output is correct
78 Correct 174 ms 62712 KB Output is correct
79 Correct 122 ms 55804 KB Output is correct
80 Correct 152 ms 61000 KB Output is correct
81 Correct 214 ms 69112 KB Output is correct
82 Correct 284 ms 74232 KB Output is correct
83 Correct 309 ms 76536 KB Output is correct
84 Correct 143 ms 55700 KB Output is correct
85 Correct 254 ms 71900 KB Output is correct
86 Correct 120 ms 43876 KB Output is correct
87 Correct 526 ms 59976 KB Output is correct
88 Correct 527 ms 59908 KB Output is correct
89 Correct 373 ms 60624 KB Output is correct
90 Correct 330 ms 68088 KB Output is correct
91 Correct 359 ms 65268 KB Output is correct
92 Correct 380 ms 63904 KB Output is correct
93 Correct 605 ms 72368 KB Output is correct
94 Correct 518 ms 77304 KB Output is correct
95 Correct 744 ms 80372 KB Output is correct
96 Correct 316 ms 59912 KB Output is correct
97 Correct 448 ms 68416 KB Output is correct