Codeforces 976C problem solving Report

Topic 1

http://codeforces.com/contest/976/problem/C

Two, train of thought

Sort data:
(1) From small to large rows according to the number on the left;
(2) If the number on the left side is equal, the number on the right side shall be arranged from large to small.

After sorting, if the number to the right of a number is less than or equal to the number to the right of the previous number, then the two numbers must conform to the question.
such as

2 13
2 12
1 11

After sorting, change to

1 11
2 13
2 12

Because 12 < 13, there are < 2,12 > included in its last number < 2,13 >.

Three, code

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

#define x first
#define y second
typedef pair<int, int> pt;
const int N = 300 * 1000 + 13;
int n;
pair<pt, int> a[N];

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < int(n); i++)
    {
        scanf("%d%d", &a[i].x.x, &a[i].x.y);
        a[i].y = i + 1;
    }

    // lambda function is used here, [&] means reference passing
    // First, arrange a.x.x from small to large; if a.x.x is the same, arrange a.x.y from large to small
    sort(a, a + n, [&](pair<pt, int> a, pair<pt, int> b)
                    {
                        if (a.x.x != b.x.x)
                        {
                            return a.x.x < b.x.x;
                        }

                        return a.x.y > b.x.y;
                    });

    set<pt> cur;
    for (int i = 0; i < int(n); i++)
    {
        while (!cur.empty() && cur.begin()->x < a[i].x.x)
        {
            // If y of previous number is smaller than x of current number, remove all previous numbers from cur
            // For example, if the previous number is < 1,5 > and < 2,6 > and the current number is < 10,20 >, then remove < 1,5 > and < 2,6 >
            cur.erase(cur.begin());
        }

        // After sort ing, the x of the current number must be greater than or equal to the x of the previous number
        // If the y of the current number is less than or equal to the y of the previous number, the output result is in line with the meaning of the question
        if (!cur.empty() && (--cur.end())->x >= a[i].x.y)
        {
            printf("%d %d\n", a[i].y, (--cur.end())->y);
            return 0;
        }
        cur.insert({a[i].x.y, a[i].y});
    }

    puts("-1 -1");
    return 0;
}


TopCoder & codeforces & atcoder communication QQ group: 648202993
More content, please pay attention to WeChat official account.


wechat_public.jpg

Tags: less Lambda

Posted on Wed, 25 Mar 2020 12:37:39 -0400 by harley1387