Submission #286304

# Submission time Handle Problem Language Result Execution time Memory
286304 2020-08-30T09:34:54 Z Alexa2001 Sandwich (JOI16_sandwich) C++17
35 / 100
8000 ms 45144 KB
#include <bits/stdc++.h>

using namespace std;

const int inf = 1e9;
const int Nmax = 404;

int cells, n, m;


char a[Nmax][Nmax];
int ans[Nmax][Nmax], pending[Nmax];
bool Upper[Nmax][Nmax], Lower[Nmax][Nmax];

vector<pair<int,int>> v;
vector<pair<int,int>> edges[Nmax];
vector<int> to[2 * Nmax * Nmax];


void add_upper(int x, int y, int prv);


int code(int x, int y, int lw)
{
    return (x-1) * m + y + lw * n * m;
}

void add_lower(int x, int y, int prv = 0)
{
    if(prv) v.push_back({prv, code(x, y, 0)});

    if(Lower[x][y]) return;
    Lower[x][y] = 1;

    ++cells;

    if(a[x][y] == 'N') /// adaug dreapta
    {
        if(a[x][y+1] == 'N')
            add_lower(x, y+1, code(x, y, 0));
        else if(a[x][y+1] == 'Z')
            add_upper(x, y+1, code(x, y, 0));

        if(x - 1 >= 1)
            add_lower(x-1, y, code(x, y, 0));
    }
    else
    {
        if(a[x][y-1] == 'N')
            add_upper(x, y-1, code(x, y, 0));
        else if(a[x][y-1] == 'Z')
            add_lower(x, y-1, code(x, y, 0));

        if(x - 1 >= 1)
            add_lower(x-1, y, code(x, y, 0));
    }
}

void add_upper(int x, int y, int prv = 0)
{
    if(prv) v.push_back({prv, code(x, y, 1)});

    if(Upper[x][y]) return;
    Upper[x][y] = 1;

    ++cells;

    if(a[x][y] == 'N') /// adaug dreapta
    {
        if(a[x][y-1] == 'N')
            add_upper(x, y-1, code(x, y, 1));
        else if(a[x][y-1] == 'Z')
            add_lower(x, y-1, code(x, y, 1));

        if(x + 1 <= n)
            add_upper(x+1, y, code(x, y, 1));
    }
    else
    {
        if(a[x][y+1] == 'N')
            add_lower(x, y+1, code(x, y, 1));
        else if(a[x][y+1] == 'Z')
            add_upper(x, y+1, code(x, y, 1));

        if(x + 1 <= n)
            add_upper(x+1, y, code(x, y, 1));
    }
}

int nr, used[2*Nmax*Nmax];
bool bad;

void dfs(int node)
{
    if(used[node]) return;
    used[node] = -1;

    for(auto it : to[node])
        dfs(it);

    used[node] = ++nr;

    for(auto it : to[node])
        if(used[it] == -1) bad = 1;
}


bool cycle(int x, int y)
{
    int i, j;
    for(i=1; i<=2*n*m; ++i)
    {
        to[i].clear();
        used[i] = 0;
    }

    for(i=x; i<=y; ++i)
        for(j=0; j<edges[i].size(); ++j)
            to[edges[i][j].first].push_back(edges[i][j].second);

    nr = 0;
    bad = 0;

    for(i=1; i<=2*n*m; ++i)
        if(!used[i]) dfs(i);

    return bad;
}

void solve1(int c)
{
    int i, j;
    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            Lower[i][j] = Upper[i][j] = 0;

    cells = 0;
    bad = 0;

    for(i=1; i<=n; ++i)
    {
        add_lower(i, c);
        pending[i] = cells;
        edges[i] = v;
        v.clear();
    }

    int st, dr, mid;

    st = 1; dr = n;
    while(st <= dr)
    {
        mid = (st + dr) / 2;
        if(!cycle(1, mid)) st = mid + 1;
            else dr = mid-1;
    }

    for(i=1; i<=dr; ++i) ans[i][c] = min(ans[i][c], pending[i]);
}


void solve2(int c)
{
    int i, j;
    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            Lower[i][j] = Upper[i][j] = 0;

    cells = 0;
    bad = 0;


    for(i=n; i; --i)
    {
        add_upper(i, c);
        pending[i] = cells;
        edges[i] = v;
        v.clear();
    }


    int st, dr, mid;

    st = 1; dr = n;
    while(st <= dr)
    {
        mid = (st + dr) / 2;
        if(cycle(mid, n)) st = mid + 1;
            else dr = mid-1;
    }

    for(i=st; i<=n; ++i) ans[i][c] = min(ans[i][c], pending[i]);
}


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

    cin >> n >> m;

    int i, j;
    for(i=1; i<=n; ++i) cin >> (a[i] + 1);

    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            ans[i][j] = inf;

    for(i=1; i<=m; ++i)
        solve1(i);

    for(i=1; i<=m; ++i)
        solve2(i);

    for(i=1; i<=n; ++i)
        for(j=1; j<=m; ++j)
            cout << (ans[i][j] == inf ? -1 : 2 * ans[i][j]) << " \n"[j==m];

    return 0;
}

Compilation message

sandwich.cpp: In function 'bool cycle(int, int)':
sandwich.cpp:118:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |         for(j=0; j<edges[i].size(); ++j)
      |                  ~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8064 KB Output is correct
2 Correct 5 ms 8064 KB Output is correct
3 Correct 5 ms 8192 KB Output is correct
4 Correct 6 ms 8064 KB Output is correct
5 Correct 6 ms 8192 KB Output is correct
6 Correct 115 ms 8912 KB Output is correct
7 Correct 58 ms 9208 KB Output is correct
8 Correct 60 ms 8952 KB Output is correct
9 Correct 39 ms 8568 KB Output is correct
10 Correct 59 ms 8952 KB Output is correct
11 Correct 86 ms 9916 KB Output is correct
12 Correct 47 ms 8960 KB Output is correct
13 Correct 87 ms 9888 KB Output is correct
14 Correct 91 ms 9720 KB Output is correct
15 Correct 42 ms 8440 KB Output is correct
16 Correct 53 ms 8576 KB Output is correct
17 Correct 55 ms 8824 KB Output is correct
18 Correct 53 ms 8568 KB Output is correct
19 Correct 53 ms 8696 KB Output is correct
20 Correct 46 ms 8568 KB Output is correct
21 Correct 45 ms 8568 KB Output is correct
22 Correct 44 ms 8576 KB Output is correct
23 Correct 46 ms 8696 KB Output is correct
24 Correct 83 ms 9848 KB Output is correct
25 Correct 86 ms 10232 KB Output is correct
26 Correct 76 ms 10104 KB Output is correct
27 Correct 56 ms 8576 KB Output is correct
28 Correct 71 ms 8704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8064 KB Output is correct
2 Correct 5 ms 8064 KB Output is correct
3 Correct 5 ms 8192 KB Output is correct
4 Correct 6 ms 8064 KB Output is correct
5 Correct 6 ms 8192 KB Output is correct
6 Correct 115 ms 8912 KB Output is correct
7 Correct 58 ms 9208 KB Output is correct
8 Correct 60 ms 8952 KB Output is correct
9 Correct 39 ms 8568 KB Output is correct
10 Correct 59 ms 8952 KB Output is correct
11 Correct 86 ms 9916 KB Output is correct
12 Correct 47 ms 8960 KB Output is correct
13 Correct 87 ms 9888 KB Output is correct
14 Correct 91 ms 9720 KB Output is correct
15 Correct 42 ms 8440 KB Output is correct
16 Correct 53 ms 8576 KB Output is correct
17 Correct 55 ms 8824 KB Output is correct
18 Correct 53 ms 8568 KB Output is correct
19 Correct 53 ms 8696 KB Output is correct
20 Correct 46 ms 8568 KB Output is correct
21 Correct 45 ms 8568 KB Output is correct
22 Correct 44 ms 8576 KB Output is correct
23 Correct 46 ms 8696 KB Output is correct
24 Correct 83 ms 9848 KB Output is correct
25 Correct 86 ms 10232 KB Output is correct
26 Correct 76 ms 10104 KB Output is correct
27 Correct 56 ms 8576 KB Output is correct
28 Correct 71 ms 8704 KB Output is correct
29 Correct 18 ms 8064 KB Output is correct
30 Correct 6 ms 9216 KB Output is correct
31 Execution timed out 8032 ms 45144 KB Time limit exceeded
32 Halted 0 ms 0 KB -