1472E - Correct Placement - CodeForces Solution


binary search data structures dp sortings two pointers *1700

Please click on ads to support us..

Python Code:

T = int(input())
for t in range(T):
    n = int(input())
    lis = [0]*n
    ans = [-1]*n
    for i in range(n):
        x, y = map(int, input().split())
        lis[i] = (max(x, y), min(x, y), i)
    lis.sort()
    y2, i2 = 10**10, 0
    last_x = -1
    for x, y, i in lis:
        if last_x < x:
            min_y, min_j = y2, i2
            last_x = x
        if min_y < y:
            ans[i] = min_j+1
        if y < y2:
            y2, i2 = y, i
    print(*ans, sep=' ')

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>pii;
typedef pair<pii,ll>ppi;
#define f first
#define s second
int main()
{
	int t;cin>>t;
	while(t--)
    {
        int n;cin>>n;
        vector<ppi>vc;
        for(int i=1;i<=n;i++)
        {
            ll x,y;cin>>x>>y;
            vc.push_back(ppi(pii(x,-y),i));
            vc.push_back(ppi(pii(y,-x),i));
        }
        sort(vc.begin(),vc.end());
        vector<ll>ans;
        for(int i=0;i<n;i++){ans.push_back(-1);}
        ll mn=LONG_MAX;
        ll el=-1;
        for(auto x:vc)
        {
            ll h=-x.f.s;
            ll i=x.s;
            if(h>mn){ans[i-1]=el;}
            else{mn=h;el=i;}
        }
        for(auto x:ans){cout<<x<<' ';}
        cout<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap