P2352 team God's book (differential)

topic

P2352 team's new book God

Resolve

Title meaning

You n intervals, selects one number x, so that \ (x \ x Times covered section of the number \) Maximum

And this problem is almost
difference, discrete look, in the interval \ (l \) at \ (+ 1 \) , \ (r + 1 \) at \ (--1 \) , the difference is that we ask for is the largest under the product, apparently the same number of cover, \ (i \) greater, the greater the answer, so we \ (r \) at \ (+ 0 \) , indicating that the position is not involved in the operation, but to contribute answers and then sort sweep again on it

Code

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;

int n, ans, mx, sum;

struct node {
    int a, b;
    bool operator <(const node &oth) const {
        return a < oth.a;
    }
} e[N];

signed main() {
    cin >> n;
    for (int i = 1, x, y; i <= n; ++i) {
        cin >> x >> y;
        e[++mx] = (node) {x, 1};
        e[++mx] = (node) {y, 0};
        e[++mx] = (node) {y + 1, -1};
    }
    sort(e + 1, e + 1 + mx);
    for (int i = 1; i <= mx; ++i) {
        sum += e[i].b;
        ans = max(ans, sum * e[i].a);
    }
    cout << ans;
}

Guess you like

Origin www.cnblogs.com/lykkk/p/11754625.html