Submission #1020291

# Submission time Handle Problem Language Result Execution time Memory
1020291 2024-07-11T19:46:36 Z parentoni Mecho (IOI09_mecho) C++17
100 / 100
198 ms 11664 KB
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define print(x) for (auto el: x) cout << el << " "; cout << '\n'
#define f first
#define s second

using ll = long long;
using llb = long double;
using vl = vector<ll>;
using pll = pair<ll,ll>;

// functions
void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }

// geometry
const double PI = 3.14159265358979323846;
double DEG_to_RAD (double d) {return d*PI/180.0;}
double RAD_to_DEG (double r) {return r*180.0/ PI;}

// values
const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MAX_N = 801;

ll dx[4] = {1,-1,0,0}, dy[4] = {0,0,1,-1};

pll m, house;
ll n, s, db[MAX_N][MAX_N]; queue<pll> qb;
bitset<MAX_N> b[MAX_N]; // board;

bool possible(ll min) {

  vector<vl> d(n, vl(n, -1));
  queue<pll> q; q.push(m); d[m.f][m.s] = 0;

  if (min >= db[m.f][m.s]) return false;
  while(q.size()) {
    pll a = q.front(); q.pop();
    for (int i=0;i<4;i++) {
      ll x = a.f + dx[i], y = a.s + dy[i], c = d[a.f][a.s] + 1;
      if (x < 0 || x >= n || y < 0 || y >= n) continue;
      if (x == house.f && y == house.s) return true;
      if (!b[x][y] || d[x][y] != -1) continue;
      if (c/s + min >= db[x][y] && db[x][y] != -1) continue;
      d[x][y] = c;
      q.push({x,y});
    }
  }

  return false;
}

ll last_true(ll lo, ll hi, function<bool(ll)> f) {
  ll ans = -1;

  while(lo <= hi) {
    ll med = (hi-lo)/2 + lo;
    if (f(med)) {
      lo = med + 1;
      ans = med;
    } else {
      hi = med - 1;
    }
  }

  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> s;
  for (auto &row: db) for (auto &el: row) el = -1;

  for (int i=0;i<n;i++) {
    string t; cin >> t;
    for (int j=0;j<n;j++) {
      b[i][j] = (t[j] == 'G' || t[j] == 'M');
      if (t[j] == 'H') {
        db[i][j] = 0;
        qb.push({i,j});
      }

      if (t[j] == 'M') {
        m = {i,j};
      } 

      if (t[j] == 'D') {
        house = {i,j};
      }
    }
  }

  while(qb.size()) {
    pll a = qb.front(); qb.pop();
    for (int i=0;i<4;i++) {
      ll x = a.f + dx[i], y = a.s + dy[i];
      if (x < 0 || x >= n || y < 0 || y >= n) continue;
      if (!b[x][y] || db[x][y] != -1) continue;
      db[x][y] = db[a.f][a.s]+1;
      qb.push({x,y});
    }
  }

  //for (int i=0;i<n;i++) {
  //  for (int j=0;j<n;j++) cout << db[i][j] << " ";
  //  cout << endl;
  //}

  cout << last_true(0, 1e17, possible) << endl;
}

Compilation message

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:15:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 | void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:15:71: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 | void setIO(string s) {freopen((s + ".in").c_str(), "r", stdin);freopen((s + ".out").c_str(), "w", stdout);}
      |                                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5468 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 2 ms 5468 KB Output is correct
4 Correct 2 ms 5468 KB Output is correct
5 Correct 2 ms 5468 KB Output is correct
6 Correct 2 ms 5468 KB Output is correct
7 Correct 137 ms 11548 KB Output is correct
8 Correct 2 ms 5468 KB Output is correct
9 Correct 2 ms 5468 KB Output is correct
10 Correct 2 ms 5464 KB Output is correct
11 Correct 2 ms 5468 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5464 KB Output is correct
14 Correct 2 ms 5468 KB Output is correct
15 Correct 2 ms 5468 KB Output is correct
16 Correct 2 ms 5468 KB Output is correct
17 Correct 2 ms 5468 KB Output is correct
18 Correct 2 ms 5468 KB Output is correct
19 Correct 2 ms 5364 KB Output is correct
20 Correct 2 ms 5468 KB Output is correct
21 Correct 2 ms 5468 KB Output is correct
22 Correct 2 ms 5468 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 2 ms 5468 KB Output is correct
25 Correct 2 ms 5296 KB Output is correct
26 Correct 3 ms 5468 KB Output is correct
27 Correct 3 ms 5464 KB Output is correct
28 Correct 2 ms 5468 KB Output is correct
29 Correct 2 ms 5468 KB Output is correct
30 Correct 2 ms 5468 KB Output is correct
31 Correct 2 ms 5464 KB Output is correct
32 Correct 2 ms 5468 KB Output is correct
33 Correct 17 ms 6804 KB Output is correct
34 Correct 17 ms 6604 KB Output is correct
35 Correct 27 ms 6624 KB Output is correct
36 Correct 23 ms 7132 KB Output is correct
37 Correct 22 ms 7124 KB Output is correct
38 Correct 36 ms 6964 KB Output is correct
39 Correct 27 ms 7512 KB Output is correct
40 Correct 27 ms 7528 KB Output is correct
41 Correct 59 ms 7356 KB Output is correct
42 Correct 34 ms 7932 KB Output is correct
43 Correct 41 ms 8096 KB Output is correct
44 Correct 58 ms 7916 KB Output is correct
45 Correct 40 ms 8360 KB Output is correct
46 Correct 41 ms 8356 KB Output is correct
47 Correct 70 ms 8328 KB Output is correct
48 Correct 65 ms 8892 KB Output is correct
49 Correct 48 ms 8904 KB Output is correct
50 Correct 82 ms 8900 KB Output is correct
51 Correct 56 ms 9564 KB Output is correct
52 Correct 56 ms 9580 KB Output is correct
53 Correct 95 ms 9452 KB Output is correct
54 Correct 70 ms 10108 KB Output is correct
55 Correct 81 ms 10068 KB Output is correct
56 Correct 142 ms 10140 KB Output is correct
57 Correct 76 ms 10816 KB Output is correct
58 Correct 80 ms 10968 KB Output is correct
59 Correct 138 ms 10700 KB Output is correct
60 Correct 92 ms 11476 KB Output is correct
61 Correct 113 ms 11336 KB Output is correct
62 Correct 158 ms 11440 KB Output is correct
63 Correct 149 ms 11540 KB Output is correct
64 Correct 187 ms 11628 KB Output is correct
65 Correct 198 ms 11584 KB Output is correct
66 Correct 165 ms 11576 KB Output is correct
67 Correct 186 ms 11440 KB Output is correct
68 Correct 117 ms 11616 KB Output is correct
69 Correct 107 ms 11440 KB Output is correct
70 Correct 107 ms 11464 KB Output is correct
71 Correct 102 ms 11396 KB Output is correct
72 Correct 125 ms 11424 KB Output is correct
73 Correct 93 ms 11664 KB Output is correct
74 Correct 114 ms 11508 KB Output is correct
75 Correct 120 ms 11496 KB Output is correct
76 Correct 112 ms 11480 KB Output is correct
77 Correct 141 ms 11452 KB Output is correct
78 Correct 120 ms 11500 KB Output is correct
79 Correct 112 ms 11452 KB Output is correct
80 Correct 121 ms 11472 KB Output is correct
81 Correct 122 ms 11536 KB Output is correct
82 Correct 127 ms 11640 KB Output is correct
83 Correct 123 ms 11536 KB Output is correct
84 Correct 123 ms 11640 KB Output is correct
85 Correct 130 ms 11512 KB Output is correct
86 Correct 121 ms 11500 KB Output is correct
87 Correct 138 ms 11328 KB Output is correct
88 Correct 130 ms 11524 KB Output is correct
89 Correct 131 ms 11624 KB Output is correct
90 Correct 129 ms 11504 KB Output is correct
91 Correct 182 ms 11500 KB Output is correct
92 Correct 127 ms 11556 KB Output is correct