Submission #147823

#TimeUsernameProblemLanguageResultExecution timeMemory
147823Alexa2001Virus Experiment (JOI19_virus)C++17
100 / 100
453 ms23040 KiB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 808, inf = 1e9;

int ans[Nmax][Nmax];
int sz[Nmax * Nmax], t[Nmax * Nmax];
int A[Nmax][Nmax], last_visited[Nmax][Nmax];
string moves;
int L, N, M, id_bfs = 0;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
string directions = "NSWE";
int best[20];

void max_to(int &x, int y) { if(x < y) x = y; }
int get_comp(int x, int y) { return (x-1) * M + y; } 

int boss(int x)
{
    if(x == t[x]) return x;
    return t[x] = boss(t[x]);
}


void prep()
{
    int i;
    moves = moves + moves;
    
    for(auto &c : moves)
        c = find(directions.begin(), directions.end(), c) - directions.begin();

    for(i=0; i<(1<<4); ++i)
    {
        int j, nr = 0;
        
        for(j=0; j<moves.size(); ++j)
            if(i & (1<<moves[j])) ++nr;
                else max_to(best[i], nr), nr = 0;
        

        for(j=0; j<moves.size(); ++j)
            if(i & (1<<moves[j])) ++nr;
                else break;
        max_to(best[i], nr);
        
        if(best[i] > moves.size() / 2) best[i] = inf;
    }
}

void init()
{
    int i, j;
    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
            if(A[i][j])
            {
                ans[i][j] = inf;
                int now = get_comp(i, j);
                t[now] = now;
                sz[now] = 1;
            }
}

void connect(int a, int b)
{
    if(sz[b] == -1)
    {
        sz[a] = -1;
        return;
    }

    t[a] = b;
    sz[b] += sz[a];
    return;
}    

bool bfs(int x, int y)
{
    ++id_bfs;

    queue<int> q;
    vector<pair<int,int>> visited;
    
    visited.push_back({x, y});
    last_visited[x][y] = id_bfs;

    int id = get_comp(x, y);
    int cursor = 0;

    while(cursor < visited.size())
    {
        tie(x, y) = visited[cursor++]; 
        
        int i;
        for(i=0; i<4; ++i)
        {
            int nx, ny;
            nx = x + dx[i];
            ny = y + dy[i];
            if(A[nx][ny] == 0) continue;
            
            int j; int mask = 0;

            for(j=0; j<4; ++j)
                if(last_visited[nx + dx[j]][ny + dy[j]] == id_bfs) mask |= (1<<j);
            
            if(A[nx][ny] > best[mask]) continue; 
            
            int nid = boss(get_comp(nx, ny));
                
            if(nid == id)
            {
                if(id_bfs == last_visited[nx][ny]) continue;
                visited.push_back({nx, ny});
                last_visited[nx][ny] = id_bfs;
            }
            else 
            {
                connect(id, nid);
                return 1;
            }
        }
    }
    
    sz[id] = -1;
    for(auto it : visited) ans[it.first][it.second] = visited.size();
    return 0;
}

bool solve()
{
    int i, j;
    bool done = 0;

    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
        {
            if(A[i][j] == 0) continue;
            int curr = get_comp(i, j);

            if(boss(curr) == curr && sz[curr] != -1) 
                done |= bfs(i, j);
        }
    return done;
}

int main()
{
 //   freopen("virus.in", "r", stdin);
    cin.tie(0); cin.sync_with_stdio(false);

    cin >> L >> N >> M;
    cin >> moves;

    int i, j;
    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
            cin >> A[i][j];

    prep();
    init();
    
    bool ok = 1;
    while(ok) ok = solve();

    int mn = inf, cnt = 0;

    for(i=1; i<=N; ++i)
        for(j=1; j<=M; ++j)
            if(A[i][j])
            {
                if(mn > ans[i][j]) mn = ans[i][j], cnt = 1;
                    else if(mn == ans[i][j]) ++cnt;
            }

    cout << mn << '\n' << cnt << '\n';
    return 0;
}

Compilation message (stderr)

virus.cpp: In function 'void prep()':
virus.cpp:40:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(j=0; j<moves.size(); ++j)
                  ~^~~~~~~~~~~~~
virus.cpp:45:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(j=0; j<moves.size(); ++j)
                  ~^~~~~~~~~~~~~
virus.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(best[i] > moves.size() / 2) best[i] = inf;
            ~~~~~~~~^~~~~~~~~~~~~~~~~~
virus.cpp: In function 'bool bfs(int, int)':
virus.cpp:94:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while(cursor < visited.size())
           ~~~~~~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...