Cod sursa(job #2932716)

Utilizator Luka77Anastase Luca George Luka77 Data 3 noiembrie 2022 19:15:42
Problema Tribute Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("tribute.in");
ofstream fout("tribute.out");

int n, dx, dy;
vector<pair<int,int>>coord;

inline int distManhX()
{
    int ans = 0;
    int st = 0, dr = coord.size()-1;
    while(st < dr && coord[dr].first - coord[st].first > dx)
    {
        ans += coord[dr].first - coord[st].first - dx;
        st++;
        dr--;
    }
    return ans;
}

inline int distManhY()
{
    int ans = 0;
    int st = 0, dr = coord.size()-1;
    while(st < dr && coord[dr].second - coord[st].second > dy)
    {
        ans += coord[dr].second - coord[st].second - dy;
        st++;
        dr--;
    }
    return ans;
}


inline bool comp(pair<int,int>x, pair<int,int>y)
{
    if(x.first == y.first)
        return x.second < y.second;
    return x.first < y.first;
}

inline void solve()
{
    sort(coord.begin(), coord.end(), comp);
    fout << (long long) distManhX() + distManhY();
}

int main()
{
    fin >> n >> dx >> dy;
    for(int i=1;i<=n;++i)
    {
        int x, y;
        fin >> x >> y;
        coord.push_back({x,y});
    }
    solve();
}