Cod sursa(job #2943720)

Utilizator YosifIosif Andrei Stefan Yosif Data 21 noiembrie 2022 16:12:45
Problema Tribute Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 7.72 kb
#include <bits/stdc++.h>
using namespace std;

class input {

private:

    FILE* fin;
    const int sz = 10000;
    char* buff, c;
    int p;
    const char stop = char(-51);

    char read()
    {
        if (p == sz)
        {
            fread(buff, 1, sz, fin);
            p = 0;
            return buff[p++];
        }
        else
            return buff[p++];
    }

    bool digit(char c)
    {
        return c >= '0' && c <= '9';
    }

public:

    input(const char* name)
    {
        c = '-';
        buff = new char[sz];
        p = 0;
        fin = fopen(name, "r");
        fread(buff, 1, sz, fin);
    }

    void close()
    {
        fclose(fin);
    }

    void open(const char* name)
    {
        c = '-';
        p = sz;
        fin = fopen(name, "r");
    }

    bool eof()
    {
        int i = p;

        while (true)
        {
            while (i < sz && (buff[i] == ' ' || buff[i] == '\n'))
                i++;

            if (i != sz)
            {
                if (buff[i] == stop)
                    return 1;
                return 0;
            }
            else
            {
                fread(buff, 1, sz, fin);
                p = 0;
                i = 0;
            }
        }
    }

    input& operator >> (int& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (unsigned long long& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;
        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (char& ch)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        ch = c;

        return *this;
    }

    input& operator >> (string& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = "";

        while (c != ' ' && c != '\n' && c != '\0')
            n += c, c = read();

        return *this;
    }

    input& operator >> (double& n)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        while (digit(c))
            n = n * 10 + (c - '0'), c = read();

        if (c != '.')
            return *this;

        c = read();

        double p10 = 10;

        while (digit(c))
            n = n + double(c - '0') / p10, c = read(), p10 *= 10;

        return *this;
    }

    input& operator >> (char* s)
    {
        c = read();

        while (c == ' ' || c == '\n')
            c = read();

        while (c != ' ' && c != '\n')
            *s = c, s++, c = read();

        *s = '\0';

        return *this;
    }

    void getline(string& s)
    {
        s = "";

        c = read();

        while (c == ' ')
            c = read();

        while (c != '\n')
            s += c, c = read();
    }
};
class output {

private:
    FILE* fout;
    const int sz = 10000;
    char* buff;
    int p;

    void write(char ch)
    {
        if (p == sz)
        {
            fwrite(buff, 1, sz, fout);
            p = 0;
            buff[p++] = ch;
        }
        else
            buff[p++] = ch;
    }

public:

    output(const char* name)
    {
        buff = new char[sz];
        p = 0;
        fout = fopen(name, "w");
    }

    ~output()
    {
        fwrite(buff, 1, p, fout);
    }

    output& operator << (int n)
    {
        if (n < 0)
            write('-'), n *= -1;

        if (n == 0)
        {
            write(n + '0');
            return *this;
        }

        if (n / 10 != 0)
            *this << (n / 10);

        write(('0' + (n % 10)));

        return *this;
    }

    output& operator << (long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (unsigned long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }

        if (n < 10)
        {
            write(n + '0');
            return *this;
        }

        *this << (n / 10);
        write((n % 10) + '0');

        return *this;
    }

    output& operator << (char c)
    {
        write(c);
        return *this;
    }

    void precision(double n, int x)
    {
        *this << int(n);

        if (!x)
            return;

        write('.');

        n -= int(n);
        n *= 10;

        for (int i = 1; i <= x; i++)
        {
            *this << int(n);
            n -= int(n);
            n *= 10;
        }
    }

    output& operator << (string n)
    {
        for (auto i : n)
            write(i);
        return *this;
    }
};

input fin("tribute.in");
output fout("tribute.out");

typedef long long ll;
int n, lg, lat;
pair <int, int> v[50001];
ll mini = LLONG_MAX;

void solve(ll x, ll y, ll lg, ll lat, ll mLg, ll mLat)
{
    ll x1, y1, x2, y2;

    x1 = x - mLg;
    y1 = y - mLat;
    x2 = x + (lg - mLg);
    y2 = y + (lat - mLat);;

    ll sum = 0;

    for (int i = 1; i <= n; i++)
    {
        if (v[i].first >= x1 && v[i].first <= x2 && v[i].second >= y1 && v[i].second <= y2)
            continue;

        if (v[i].first < x1 && v[i].second >= y1 && v[i].second <= y2)
            sum += x1 - v[i].first;
        else if (v[i].first > x2 && v[i].second >= y1 && v[i].second <= y2)
            sum += v[i].first - x2;
        else if (v[i].second < y1 && v[i].first >= x1 && v[i].first <= x2)
            sum += y1 - v[i].second;
        else if (v[i].second > y2 && v[i].first >= x1 && v[i].first <= x2)
            sum += v[i].second - y2;
        else if (v[i].first < x1 && v[i].second < y1)
            sum += x1 - v[i].first + y1 - v[i].second;
        else if (v[i].first < x1 && v[i].second > y1)
            sum += x1 - v[i].first + v[i].second - y1;
        else if (v[i].first > x2 && v[i].second < y1)
            sum += v[i].first - x2 + y1 - v[i].second;
        else
            sum += v[i].first - x2 + v[i].second - y2;
    }

    mini = min(mini, sum);
}

int main()
{
    fin >> n >> lg >> lat;

    ll sx = 0, sy = 0;

    for (int i = 1; i <= n; i++)
    {
        int x, y;
        fin >> x >> y;
        v[i] = { x, y };

        sx += x;
        sy += y;
    }

    sx /= n;
    sy /= n;

    ll mLg = lg / 2;
    ll mLat = lat / 2;

    for (ll i = sx; i <= sx + 1; i++)
        for (ll j = sy; j <= sy + 1; j++)
        {
            solve(i, j, lg, lat, mLg, mLat);
            solve(i, j, lat, lg, mLat, mLg);
            solve(i, j, lg, lat, mLg + 1, mLat + 1);
            solve(i, j, lat, lg, mLat + 1, mLg + 1);
        }

    fout << mini;

    return 0;
}