1044A. The Tower is Going Home - Codeforces Solution C++

  Problem Link : 1044A. The Tower is Going Home 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

vector<ll> vert;
vector<pair<ll,pair<ll,ll> > > horz;


bool comp(pair<ll,pair<ll,ll> > x, pair<ll,pair<ll,ll> > y)
{
	return x.second.second>y.second.second;
}

bool comp2(ll x,ll y)
{
    return x>y;
}

int main()
{
	ll n,m;
	cin>>n>>m;
	ll u,v,w;
	ll ansC=0;
	for(ll i=1;i<=n;i++)
	{
		cin>>v;
		vert.push_back(v);
	}
	for(ll i=1;i<=m;i++)
	{
		cin>>u>>v>>w;
		if(u!=1)
			continue;
		else if(v==pow(10,9))
			ansC++;
		else
			horz.push_back(make_pair(w,make_pair(u,v)));
	}

	sort(vert.begin(),vert.end(),comp2);
	sort(horz.begin(),horz.end(),comp);

	ll i=0,j=0;
	ll cnt=0;
	ll ans=pow(10,6);
	ll s=vert.size();
	ans=min(ans,s);
	while(i<horz.size() && j<s)
	{
		if(horz[i].second.second < vert[j])
		{
			ans=min(ans,s-j-1+cnt);
			j++;
			continue;
		}
		cnt++;
		i++;
	}
	if(j<vert.size())
	{
		ans=min(ans,cnt);
	}

	cout<<ans+ansC;

	

}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!