Lighting Rectangle CodeChef SnackDown 2021 Advance Practice Contest
Question
You are given an axis-aligned rectangle in a 2D Cartesian plane. The bottom left corner of this rectangle has coordinates (0,0) and the top right corner has coordinates (N−1,N−1). You are also given K light sources; each light source is a point inside or on the perimeter of the rectangle.
For each light source, let's divide the plane into four quadrants by a horizontal and a vertical line passing through this light source. The light source can only illuminate one of these quadrants (including its border, i.e. the point containing the light source and two half-lines), but the quadrants illuminated by different light sources may be different.
You want to assign a quadrant to each light source in such a way that when they illuminate their respective quadrants, the entire rectangle (including its perimeter) is illuminated. Find out whether it is possible to assign quadrants to light sources in such a way.
Input
- The first line of the input contains an integer T denoting the number of test cases. The description of the test cases follows.
- The first line of each test case contains two space-separated integers K and N.
- Each of the next K lines contains two space-separated integers x and y denoting a light source with coordinates (x,y).
Output
For each test case, print a single line containing the string "yes"
if it is possible to illuminate the whole rectangle or "no"
if it is impossible.
Constraints
- 1≤T≤5,000
- 1≤K≤100
- 1≤N≤109
- 0≤x,y≤N−1
- no two light sources coincide
Example Input
Example Output
Program Code in C++
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define Max 100000000000000
#define min_heap priority_queue <ll, vector<ll>, greater<ll> >
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main()
{
ll t;
cin>>t;
while(t--){
ll k,n;
cin>>k>>n;
ll x[k],y[k];
for(ll i=0;i<k;i++) cin>>x[i]>>y[i];
if(k>=4){
cout<<"yes"<<endl;
continue;
}
ll p=0,q=0,chk=0;
ll u[]={0,0,n-1,n-1};
ll v[]={0,n-1,0,n-1};
for(ll i=0;i<k;i++){
for(ll j=0;j<4;j++){
if(x[i]==u[j] && y[i]==v[j]) chk=1;
}
if(x[i]==0 || x[i]==n-1) p++;
if(y[i]==0 || y[i]==n-1) q++;
}
if(chk) cout<<"yes"<<endl;
else if(p>=2 || q>=2) cout<<"yes"<<endl;
else if(p+q>=2 && k==3) cout<<"yes"<<endl;
else if(p+q==0 || k<3) cout<<"no"<<endl;
else{
pair<ll,ll> c[3];
for(ll i=0;i<3;i++) c[i]={x[i],y[i]};
if(p) for(ll i=0;i<3;i++) swap(c[i].first,c[i].second);
sort(c,c+3);
if(c[0].second==0 || c[0].second==n-1 || c[2].second==0 || c[2].second==n-1 || c[0].first==c[1].first || c[1].first==c[2].first) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}
return 0;
}
Suggested Links
More information
CodeChef is one of the largest online coding platform and SnackDown is one of it's of grandest programs held ever year. SnackDown is a global programming event that invites programmers all over the world to participate in India's most prestigious multi-round programming competition. SnackDown is open to everyone who has a knack in programming.
This question that is Lighting Rectangle is of CodeChef SnackDown 2021 Advanced Practice Contest. Here you will get a brief explanation of the problem and after reading the explanation if you are still stuck and have no clue to understand the problem then you can visit the solution of the question given above.
Hope you learnt something from this explanation and solution. Code it guys, practice coding more and more.
Comments
Post a Comment