Cod sursa(job #2534971)

Utilizator ptudortudor P ptudor Data 31 ianuarie 2020 11:19:28
Problema Tribute Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <bits/stdc++.h>
using namespace std;

ifstream in("tribute.in");
ofstream out("tribute.out");
int n,dy,dx,st[60005],dr[60005];
pair<int,int> a[50005];
int get_x()
{int i,j;
   sort(a + 1, a + 1 + n);
   int 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";
*/
   int cost_x = 1000000005;
   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<int,int> a, pair<int,int> b)
{
   return a.second < b.second;
}
int get_y()
{
   int i,j;
   int 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";
*/
   int 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()
{int i,j;
   /// luam vectorii si ii rezolvam

   int cost_x = get_x();
   int cost_y = get_y();

   out << cost_x + cost_y << "\n";
}
int main()
{int 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.
*/