Cod sursa(job #2601372)

Utilizator ptudortudor P ptudor Data 14 aprilie 2020 13:19:24
Problema Tribute Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

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

ll n,dy,dx,st[60005],dr[60005],st2[50005],dr2[50005];
pair<ll,ll> a[50005];
ll get_x()
{ll i,j;
   ll c = 0,xmax = 50005;
   i = 1;
   for (i = 1; i <= xmax; i++) st[i] = dr[i] = 0;

   for (i = 1; i <= n; i++) st[a[i].first]++,dr[a[i].first]++;

   for (j = 1; j <= xmax; j++) st[j] += st[j - 1];
   for (j = 1; j <= xmax; j++) st2[j] = st[j - 1] + st2[j - 1];

   for (j = xmax - 1; j >= 0; j--) dr[j] += dr[j + 1];
   for (j = xmax - 1; j >= 0; j--) dr2[j] = dr[j + 1] + dr2[j + 1];

   ll cost_x = 1000000000005;
   for (j = 0; j <= xmax - dx; j++)
      cost_x = min(cost_x , st2[j] + dr2[j + dx]);
   return cost_x;
}
int comp(pair<ll,ll> a, pair<ll,ll> b)
{
   return a.second < b.second;
}
ll get_y()
{
   ll i,j;
   ll c = 0,ymax;
   i = 1;
   ymax = 50005,c = 0,i = 1;

    for (i = 1; i <= ymax; i++) st[i] = dr[i] = st2[i] = dr2[i] = 0;

   for (i = 1; i <= n; i++) st[a[i].second]++,dr[a[i].second]++;
   for (j = 1; j <= ymax; j++) st[j] += st[j - 1];
   for (j = 1; j <= ymax; j++) st2[j] = st[j - 1] + st2[j - 1];
   for (j = ymax - 1; j >= 0; j--) dr[j] += dr[j + 1];
   for (j = ymax - 1; j >= 0; j--) dr2[j] = dr[j + 1] + dr2[j + 1];

   ll cost_y = 1000000005;
   for (j = 0; j <= ymax - dy; j++)
      cost_y = min(cost_y , st2[j] + dr2[j + dy]);
   return cost_y;
}
void solve()
{ll i,j;
   ll cost_x = get_x();
   ll cost_y = get_y();
   out << cost_x + cost_y << "\n";
}
int main()
{ll i,j;
   in >> n >> dx >> dy;
   for (i = 1; i <= n; i++)
      in >> a[i].first >> a[i].second,
      a[i].first++,a[i].second++;
   solve();
}