Submission #547641

# Submission time Handle Problem Language Result Execution time Memory
547641 2022-04-11T06:53:39 Z pancankes Mecho (IOI09_mecho) C++17
100 / 100
243 ms 12048 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <cmath>
using namespace std;
#define int long long
const int INF=1e18;
const int MAXN=801;

int n,s,ans=0;
string grid[MAXN];
int bee[MAXN][MAXN],dist[MAXN][MAXN];
vector<pair<int,int>> hives;
pair<int,int> start,dest;

int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
bool inside(int x, int y) {
	return (x > -1 && x < n && y > -1 && y < n && grid[x][y] != 'T' && grid[x][y]!='D');
}

void setIO(string name="") { 
    ios_base::sync_with_stdio(0); cin.tie(0); 
    if (!name.empty()) {
        freopen((name+".in").c_str(), "r", stdin); 
        freopen((name+".out").c_str(), "w", stdout);
    }
}

// check if after eating for t seconds,
// is it possible to reach the destination
bool check(int t)
{
    for (int i=0; i<n; i++)
        for (int j=0; j<n; j++)
            dist[i][j]=INF;
    queue<pair<int,int>>q;
    if (bee[start.first][start.second]>t) q.push(start);
    dist[start.first][start.second]=0;
    while (!q.empty())
    {
        pair<int,int> c=q.front();
        q.pop();

        for (int i=0; i<4; i++)
        {
            int x=c.first+dx[i],y=c.second+dy[i];
            // if i can usually get there in 7 step
            // taking 2 steps a minute i can get there in ceil(7/2) steps
            if (inside(x,y)&&dist[x][y]==INF&&(bee[x][y]-t)>(dist[c.first][c.second]+1)/s)
            {
                dist[x][y]=dist[c.first][c.second]+1;
                q.push({x,y});
            }
        }
    }
    // cout << t << endl;
    // for (int i=0; i<n; i++) {for (int j=0; j<n; j++) cout << dist[i][j] << " "; cout << endl;}
    bool reached=false;
    for (int i=0; i<4; i++)
    {
        int x=dest.first+dx[i],y=dest.second+dy[i];
        if (inside(x,y)&&dist[x][y]!=INF&&(bee[x][y]-t)>(dist[x][y])/s)
            reached=true;
    }
    return reached;
}

int32_t main()
{
    cin >> n >> s;
    for (int i=0; i<n; i++) cin >> grid[i];
    for (int i=0; i<n; i++)
        for (int j=0; j<n; j++)
        {
            if (grid[i][j]=='M') start={i,j};
            if (grid[i][j]=='D') dest={i,j};
            if (grid[i][j]=='H') hives.push_back({i,j});
            bee[i][j]=INF;
        }
    // multisource bfs to find the distances of cells from hive
    queue<pair<int,int>> q;
    for (int i=0; i<hives.size(); i++) {
        q.push(hives[i]);
        bee[hives[i].first][hives[i].second]=0;
    }
    while (!q.empty())
    {
        pair<int,int> c=q.front();
        q.pop();
        for (int i=0; i<4; i++)
        {
            int x=c.first+dx[i],y=c.second+dy[i];
            if (inside(x,y)&&grid[x][y]!='D'&&bee[x][y]==INF)
            {
                bee[x][y]=bee[c.first][c.second]+1;
                q.push({x,y});
            }
        }
    }
    // for (int i=0; i<n; i++) {for (int j=0; j<n; j++) cout << bee[i][j] << " "; cout << endl;} cout << endl;
    int lo=0,hi=n*n;
    while (lo<=hi)
    {
        int mid=lo+(hi-lo)/2;
        // cout << mid << endl;
        if (check(mid))
        {
            ans=max(ans,mid);
            lo=mid+1;
        }
        else hi=mid-1;
    }
    if (!check(0)) cout << -1 << endl;
    else cout << ans << endl;
}

Compilation message

mecho.cpp: In function 'int32_t main()':
mecho.cpp:85:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for (int i=0; i<hives.size(); i++) {
      |                   ~^~~~~~~~~~~~~
mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen((name+".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen((name+".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 112 ms 11712 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 848 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 852 KB Output is correct
28 Correct 1 ms 852 KB Output is correct
29 Correct 1 ms 980 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 1 ms 984 KB Output is correct
33 Correct 9 ms 4960 KB Output is correct
34 Correct 9 ms 4948 KB Output is correct
35 Correct 31 ms 5004 KB Output is correct
36 Correct 14 ms 5588 KB Output is correct
37 Correct 11 ms 5588 KB Output is correct
38 Correct 29 ms 5668 KB Output is correct
39 Correct 12 ms 6356 KB Output is correct
40 Correct 14 ms 6392 KB Output is correct
41 Correct 39 ms 6356 KB Output is correct
42 Correct 16 ms 7248 KB Output is correct
43 Correct 18 ms 7252 KB Output is correct
44 Correct 70 ms 7324 KB Output is correct
45 Correct 24 ms 7892 KB Output is correct
46 Correct 22 ms 7984 KB Output is correct
47 Correct 55 ms 8016 KB Output is correct
48 Correct 22 ms 8596 KB Output is correct
49 Correct 27 ms 8672 KB Output is correct
50 Correct 74 ms 8632 KB Output is correct
51 Correct 28 ms 9264 KB Output is correct
52 Correct 30 ms 9300 KB Output is correct
53 Correct 97 ms 9352 KB Output is correct
54 Correct 32 ms 9920 KB Output is correct
55 Correct 33 ms 9940 KB Output is correct
56 Correct 119 ms 10036 KB Output is correct
57 Correct 38 ms 10692 KB Output is correct
58 Correct 37 ms 10648 KB Output is correct
59 Correct 127 ms 10712 KB Output is correct
60 Correct 44 ms 11344 KB Output is correct
61 Correct 44 ms 11344 KB Output is correct
62 Correct 160 ms 11256 KB Output is correct
63 Correct 135 ms 11396 KB Output is correct
64 Correct 243 ms 11376 KB Output is correct
65 Correct 182 ms 11380 KB Output is correct
66 Correct 154 ms 11384 KB Output is correct
67 Correct 150 ms 11372 KB Output is correct
68 Correct 66 ms 11436 KB Output is correct
69 Correct 70 ms 11436 KB Output is correct
70 Correct 56 ms 11440 KB Output is correct
71 Correct 71 ms 11468 KB Output is correct
72 Correct 49 ms 11432 KB Output is correct
73 Correct 52 ms 11968 KB Output is correct
74 Correct 89 ms 11900 KB Output is correct
75 Correct 91 ms 11976 KB Output is correct
76 Correct 77 ms 12048 KB Output is correct
77 Correct 76 ms 11920 KB Output is correct
78 Correct 94 ms 11896 KB Output is correct
79 Correct 82 ms 11900 KB Output is correct
80 Correct 73 ms 11904 KB Output is correct
81 Correct 83 ms 11828 KB Output is correct
82 Correct 82 ms 11904 KB Output is correct
83 Correct 98 ms 11724 KB Output is correct
84 Correct 92 ms 11724 KB Output is correct
85 Correct 92 ms 11700 KB Output is correct
86 Correct 99 ms 11768 KB Output is correct
87 Correct 87 ms 11776 KB Output is correct
88 Correct 89 ms 11720 KB Output is correct
89 Correct 112 ms 11708 KB Output is correct
90 Correct 119 ms 11708 KB Output is correct
91 Correct 101 ms 11812 KB Output is correct
92 Correct 103 ms 11804 KB Output is correct