mardi 18 août 2020

Find the missing coordinate of rectangle

Chef has N axis-parallel rectangles in a 2D Cartesian coordinate system. These rectangles may intersect, but it is guaranteed that all their 4N vertices are pairwise distinct.

Unfortunately, Chef lost one vertex, and up until now, none of his fixes have worked (although putting an image of a point on a milk carton might not have been the greatest idea after all…). Therefore, he gave you the task of finding it! You are given the remaining 4N−1 points and you should find the missing one.

Input The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N. Then, 4N−1 lines follow. Each of these lines contains two space-separated integers x and y denoting a vertex (x,y) of some rectangle. Output For each test case, print a single line containing two space-separated integers X and Y ― the coordinates of the missing point. It can be proved that the missing point can be determined uniquely.

Constraints T≤100 1≤N≤2⋅105 |x|,|y|≤109 the sum of N over all test cases does not exceed 2⋅105 Example Input 1 2 1 1 1 2 4 6 2 1 9 6 9 3 4 3 Example Output 2 2

Problem link: https://www.codechef.com/problems/PTMSSNG

my approach: I have created a frequency array for x and y coordinates and then calculated the point which is coming odd no. of times.

    #include <iostream>
using namespace std;

int main() {
    // your code goes here
    int t;
    cin>>t;
    while(t--)
    {
        long int n;
        cin>>n;
        long long int a[4*n-1][2];
        long long int xm,ym,x,y;
        for(int i=0;i<4*n-1;i++)
        {
            cin>>a[i][0]>>a[i][1];
            if(i==0)
            {
                xm=abs(a[i][0]);
                ym=abs(a[i][1]);
            }
            if(i>0)
            {
                if(abs(a[i][0])>xm)
                {
                    xm=abs(a[i][0]);
                }
                if(abs(a[i][1])>ym)
                {
                    ym=abs(a[i][1]);
                }
            }
        }
        long long int frqx[xm+1],frqy[ym+1];
        for(long long int i=0;i<xm+1;i++)
        {
            frqx[i]=0;
        }
        for(long long int j=0;j<ym+1;j++)
        {
            frqy[j]=0;
        }
        for(long long int i=0;i<4*n-1;i++)
        {
            frqx[a[i][0]]+=1;
            frqy[a[i][1]]+=1;
        }
        for(long long int i=0;i<xm+1;i++)
        {
            if(frqx[i]>0 && frqx[i]%2>0)
            {
                x=i;
                break;
            }
        }
        for(long long int j=0;j<ym+1;j++)
        {
            if(frqy[j]>0 && frqy[j]%2>0)
            {
                y=j;
                break;
            }
        }
        cout<<x<<" "<<y<<"\n";
    }
    return 0;
}

My code is showing TLE for inputs <10^6

Aucun commentaire:

Enregistrer un commentaire