Cod sursa(job #1815751)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 25 noiembrie 2016 18:40:08
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.87 kb
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstring>
using namespace std;
const int NMAX = 1024;
const int VMAX = 16384;
struct point
{
    int x, y;
};
vector <point> v[VMAX + 5];
int a[NMAX + 5][NMAX + 5];
int d[NMAX + 5][NMAX + 5];
point last[NMAX + 5][NMAX + 5];
int dx[] = {0, -1, 0, 1, 0};
int dy[] = {0, 0, 1, 0, -1};

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}fin("alpin.in");
int main()
{
    freopen("alpin.out", "w", stdout);
    int n, ans = 0, dd, j, ax, ay, nx, ny;
    point nou, aux, sol;
    fin >> n;
    for (int i = 0; i <= n + 1; ++i)
        a[0][i] = a[i][0] = -1;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j)
        {
            fin >> a[i][j];
            nou.x = i; nou.y = j;
            v[a[i][j]].push_back(nou);

            d[i][j] = 1;
            last[i][j] = nou;
        }
    for (int i = VMAX; i > 0; --i)
        for(j = 0; j < v[i].size(); ++j)
        {
            ax = v[i][j].x;
            ay = v[i][j].y;
            for (dd = 1; dd <= 4; ++dd)
            {
                nx = ax + dx[dd];
                ny = ay + dy[dd];
                if (a[nx][ny] != -1 && a[nx][ny] > a[ax][ay] && d[nx][ny] + 1 > d[ax][ay])
                {
                    d[ax][ay] = d[nx][ny] + 1;
                    last[ax][ay].x = nx;
                    last[ax][ay].y = ny;
                }
            }
            if (d[ax][ay] > ans)
                ans = d[ax][ay], sol = v[i][j];
        }
    printf("%d\n", ans);
    while (sol.x != last[sol.x][sol.y].x || sol.y != last[sol.x][sol.y].y )
    {
        printf("%d %d\n", sol.x, sol.y);
        sol = last[sol.x][sol.y];
    }
    printf("%d %d\n", sol.x, sol.y);
    return 0;
}