Submission #244191

#TimeUsernameProblemLanguageResultExecution timeMemory
244191NONAMEPohlepko (COCI16_pohlepko)C++14
80 / 80
89 ms4864 KiB
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;

char c[2000][2000];

bool cmp(pair <int, int> x, pair <int, int> y) {
    return c[x.first][x.second] < c[y.first][y.second];
}

int main() {
    fast_io;

    int n, m;

    cin >> n >> m;

    for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
        cin >> c[i][j];

    vector <pair <int, int> > p, np;
    p.push_back(make_pair(0, 0));

    for (int i = 0; i < n + m - 1; ++i) {
        sort(p.begin(), p.end(), cmp);

        cout << c[p[0].first][p[0].second];

        if (i == n + m - 2)
            break;

        char ch = 'z';

        for (auto j : p) {
            int x = j.first, y = j.second;

            if (x + 1 < n)
                ch = min(ch, c[x + 1][y]);

            if (y + 1 < m)
                ch = min(ch, c[x][y + 1]);
        }

        for (auto j : p) {
            int x = j.first, y = j.second;

            if (x + 1 < n && c[x + 1][y] == ch)
                np.push_back(make_pair(x + 1, y));

            if (y + 1 < m && c[x][y + 1] == ch)
                np.push_back(make_pair(x, y + 1));
        }

        sort(np.begin(), np.end());
        np.resize(unique(np.begin(), np.end()) - np.begin());

        p = np;
        np.clear();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...