zoukankan      html  css  js  c++  java
  • [CF1472E] Correct Placement

    Description

    当一个人 A 的身高比另外一个人 B 的身高矮,且宽度比他小时,A 可以站在 B 的前面,或者当 A 身高比 B 宽度小时,且宽度比 B 身高小时,A 可以躺在 B 前面。想知道每个人是否有对应的人可以站或躺在他的前面,如果有,则输出那个人的编号,若有多个,则输出任意一个,如果没有则输出 -1。

    Solution

    将所有的数对调整成 (w le h) 不会影响结果,这样就变成了一个简单的二维偏序,扫描一遍即可。

    (太久没做题了卡半天。

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    const int N = 1000005;
    
    struct object
    {
        int h, w, id;
    
        bool operator<(const object &rhs) const
        {
            return h == rhs.h ? w < rhs.w : h < rhs.h;
        }
    };
    
    void solve()
    {
        int n;
    
        cin >> n;
    
        vector<object> obj(n + 2);
        vector<int> ans(n + 2);
    
        for (int i = 1; i <= n; i++)
        {
            int w, h;
            cin >> h >> w;
            if (h > w)
                swap(h, w);
            obj[i] = {h, w, i};
        }
    
        sort(&obj[1], &obj[n + 1]);
    
        int min_w = 1e18, min_w_id = 0, min_w_pre = 1e18, min_w_pre_id = 0;
    
        for (int i = 1; i <= n; i++)
        {
            if (min_w < obj[i].w)
            {
                ans[obj[i].id] = obj[min_w_id].id;
            }
            else
            {
                ans[obj[i].id] = -1;
            }
    
            min_w_pre = min(min_w_pre, obj[i].w);
            if (min_w_pre == obj[i].w)
                min_w_pre_id = i;
            if (obj[i].h != obj[i + 1].h)
            {
                min_w = min(min_w, min_w_pre);
                if (min_w == min_w_pre)
                {
                    min_w_id = min_w_pre_id;
                }
            }
        }
    
        for (int i = 1; i <= n; i++)
        {
            cout << ans[i] << " ";
        }
        cout << endl;
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        int t;
        cin >> t;
        while (t--)
        {
            solve();
        }
    }
    
    
  • 相关阅读:
    Pascal's Triangle II
    Pascal's Triangle
    Path Sum II
    Path Sum
    plusOne
    Divide Two Integers
    64. ZigZag Conversion
    63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
    62. Divide Two Integers
    61. Unique Paths && Unique Paths II
  • 原文地址:https://www.cnblogs.com/mollnn/p/14315088.html
Copyright © 2011-2022 走看看