1154E. Two Teams - Codeforces Solution C++

  Problem Link : 1154E. Two Teams 


✅ C++ Solution :

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

#define ll long long int

#define pp pair<int,int>
#define mp make_pair

int main()
{
	int n,k;
	cin>>n>>k;
	vector<int> ans(n);
	set<int> us;
	vector<pp> arr(n);
	for(int i=0;i<n;i++)
	{
		cin>>arr[i].first;
		arr[i].second=i;
		us.insert(i);
	}

	sort(arr.begin(),arr.end(),greater<pp>());

	int x=0;
	for(int i=0;i<n;i++)
	{
		if(us.find(arr[i].second)!=us.end())
		{
			auto it = us.find(arr[i].second);
			auto temp=it;
            if(temp!=us.begin())
            {
                temp--;
                for(int m=1;m<=k;m++)
                {
                    auto t = temp;
                    ans[*temp]=x+1;
                    if(temp==us.begin())
                    {
                        us.erase(t);
                        break;
                    }
                    temp--;
                    us.erase(t);

                }
            }
            temp=it;
            temp++;
            for(int m=1;m<=k && temp!=us.end();m++)
            {
                auto t = temp;
                ans[*temp]=x+1;
                temp++;
                us.erase(t);
            }
            ans[*it]=x+1;
            us.erase(it);
            x^=1;
		}
	}

	for(int i : ans)
        cout<<i;

}

 

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!!