Header Ad

HackerEarth Litte Jhool and World Tour problem solution

In this HackerEarth Litte Jhool and World Tour problem solution Little Jhool is just not a smart algorithmic coder, he regularly contributes to open source, too. This year, he's managed to be selected for a project in the prestigious Google Summer of Code, too. More than the project, he's happy about the money he's going to receive. He's already started day dreaming about what he will do with the money, - he's settled on for going to a tour of couple of countries. Now, he's a bit confused about which countries he wants to consider, so he buys a globe, and starts rotating it like some boss.

The globe has n different countries on it, and let's say they are labeled as 0, 1, 2, 3, 4... n-1, but since it is a globe, after n-1, country order follows again. That is to say, if there are n countries - 0, 1, 2, 3, 4, . . . , n-1, then, their arrangement would be: first 0, then 1, then 2, ..., then n-1, and then again 0...

Let us take m = 6, then {4, 5, 0} can be a valid range of countries, {2} can be a valid range, too, {3, 4, 5, 0, 1, 2} is a valid range, too. Since it is a globe, the range can wrap around itself.

Now, his girlfriend decides a range of countries. Since, his girlfriend is confusing him a lot, he just wants to pick up ONLY ONE country from a range. He does NOT want to any country to be chosen more than once for different ranges.

Given number of countries, and number of ranges (And their range!) - help Little Jhool figure out if he'll be able to do this.


HackerEarth Litte Jhool and World Tour problem solution


HackerEarth Litte Jhool and World Tour problem solution.

#include <bits/stdc++.h>

using namespace std;

typedef pair <int,int> PII;
typedef vector <int> VI;
typedef vector < PII > VPII;

int main()
{
int tc;
scanf("%d",&tc);
while (tc--)
{
int m, n;
scanf("%d %d",&m, &n);

VPII segm;
for(int i=0;i<n;i++)
{
int st, en;
scanf("%d %d",&st, &en);

if(st<=en)
{
segm.push_back(make_pair(st,en));
segm.push_back(make_pair(st+m,en+m));
}

else
segm.push_back(make_pair(st,en+m));
}

if(n>m)
{
puts("NO");
continue;
}

sort(segm.begin(),segm.end());

int T=0;
int i=0;

set < PII > que;

bool ok = true;

while(true)
{
if(que.empty())
{
if(i==segm.size())
break;
else
T = segm[i].first;
}

while(i<segm.size() && segm[i].first==T)
{
que.insert(make_pair(segm[i].second,i));
i++;
}

int ind = que.begin()->second;
que.erase(que.begin());

if(!(T>=segm[ind].first && T<=segm[ind].second))
{
ok = false;
break;
}
T++;
}
puts(ok?"YES":"NO");
}
}

Post a Comment

0 Comments