Cod sursa(job #2534976)

Utilizator ptudortudor P ptudor Data 31 ianuarie 2020 11:22:16
Problema Tribute Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.66 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];
pair<ll,ll> a[50005];
ll get_x()
{ll i,j;
   sort(a + 1, a + 1 + n);
   ll c = 0,xmax = a[n].first;
   i = 1;
   for (j = 0; j <= xmax; j++)
   {
      if (j > 0)
         st[j] = st[j - 1] + c;
      while (a[i].first <= j && i <= n)
      {
         c++;
         i++;
      }
   }

/*   cout << "st  = \n";
   for (j = 1; j <= xmax; j++)
      cout << st[j] << " "; cout << "\n";
   */c = 0,i = n;

   for (j = xmax; j >= 0; j--)
   {
      dr[j] = dr[j + 1] + c;
      while (a[i].first >= j && i > 0)
      {
         c++;
         i--;
      }
   }

  /* cout << "dr  = \n";
   for (j = 1; j <= xmax; j++)
      cout << dr[j] << " "; cout << "\n";
*/
   ll cost_x = 1000000000005;
   for (j = 0; j <= xmax - dx + 1; j++)
      cost_x = min(cost_x , st[j] + dr[j + dx]);
  /* cout << "cost_x = " << cost_x << "\n";
*/
   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;
   sort (a + 1, a + 1 + n, comp);
/*   cout << "pe verticala \n";
   cout << "sir = \n";
   for (i = 1; i <= n; i++) cout << a[i].first << "," << a[i].second << "\n";
*/
   ymax = a[n].second,c = 0,i = 1;
   for (j = 0; j <= ymax; j++)
   {
      if (j > 0)
         st[j] = st[j - 1] + c;
      while (a[i].second <= j && i <= n)
      {
         c++;
         i++;
      }
   }

  /* cout << "st  = \n";
   for (j = 0; j <= ymax; j++)
      cout << st[j] << " "; cout << "\n";*/
   c = 0,i = n;

   for (j = ymax; j >= 0; j--)
   {
      dr[j] = dr[j + 1] + c;
      while (a[i].second >= j && i > 0)
      {
         c++;
         i--;
      }
   }

  /* cout << "dr  = \n";
   for (j = 0; j <= ymax; j++)
      cout << dr[j] << " "; cout << "\n";
*/
   ll cost_y = 1000000005;
   for (j = 0; j <= ymax - dy + 1; j++)
      cost_y = min(cost_y , st[j] + dr[j + dy]);
   //cout << "cost_y = " << cost_y << "\n";

   return cost_y;
}
void solve()
{ll i,j;
   /// luam vectorii si ii rezolvam

   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;
   solve();
}
/**
Rezolvam pe axa ox mai intai
Dupa aia pe axa oy

Ce cod poate sa fie comun intre cele doua cazuri?
Pai la ambele cazuri mai intai facem sumele pe stanga si dreapta si cautam minimul.
Codul care cauta minimul poate sa fie acelasi, dar sirurile pot fi diferite.
*/