Cod sursa(job #1695188)

Utilizator cosmin79Carabet Cosmin Andrei cosmin79 Data 26 aprilie 2016 18:34:35
Problema Cadrane Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
const int NMAX = 100505;
const int LMAX = (1 << 18);
int N, M;

struct pt {
    int x, y;
};
pt A[NMAX];
vector<int> sortedX, sortedY, cnt;
vector<int> pointAtX[NMAX], pointAtY[NMAX];

struct node {
    int minV;
    int updateV;
};
node G[LMAX];

inline void sortAndUnique(vector<int>& H) {
    sort(H.begin(), H.end());
    H.resize(unique(H.begin(), H.end()) - H.begin());
}

void read() {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) {
        scanf("%d%d", &A[i].x, &A[i].y);
        sortedX.push_back(A[i].x);
        sortedY.push_back(A[i].y);
    }

    sortAndUnique(sortedX);
    sortAndUnique(sortedY);
    reverse(sortedY.begin(), sortedY.end());
    for (int i = 1; i <= N ;i++) {
        A[i].x = lower_bound(sortedX.begin(), sortedX.end(),
            A[i].x) - sortedX.begin();
        A[i].y = lower_bound(sortedY.begin(), sortedY.end(),
            A[i].y, greater<int>()) - sortedY.begin();
    }
}

int getMin() {
    int result = cnt[0];
    for (int i = 1; i < M; i++) {
        result = min(result, cnt[i]);
    }

    return result;
}

void prepare() {
    for (int i = 1; i <= N; i++) {
        pointAtX[A[i].x].push_back(A[i].y);
        pointAtY[A[i].y].push_back(A[i].x);
    }
    // cnt[i] = score if x = smallest_x and y = i
    int smallestX = 0;
    int currV = pointAtX[smallestX].size();

    M = sortedY.size();
    cnt.assign(M, 0);
    for (int i = 0; i < M; i++) {
        for (auto& x: pointAtY[i]) {
            if (x != smallestX) {
                currV++;
            }
        }
        cnt[i] = currV;
    }
    //cstr(0, M - 1, 1);
}

void solve() {
    int result = getMin();
    for (size_t i = 1; i < sortedX.size(); i++) {
        for (auto& y: pointAtX[i - 1]) {
            for (int j = y + 1; j < M; j++) {
                cnt[j]--;
            }
        }
        for (auto& y: pointAtX[i]) {
            for (int j = y - 1; j >= 0; j--) {
                cnt[j]++;
            }
        }

        result = max(result, getMin());
    }
    printf("%d\n", result);
}

int main() {
    freopen("cadrane.in", "r", stdin);
    freopen("cadrane.out", "w", stdout);

    read();
    prepare();
    solve();
    return 0;
}