Cod sursa(job #1106289)

Utilizator manutrutaEmanuel Truta manutruta Data 12 februarie 2014 18:18:42
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.95 kb
#include <cstring>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#define MAXN 200010
#define inf 0x3f3f3f3f

typedef vector< pair<int, int> >::iterator iter;

ifstream f("plicuri.in");
ofstream g("plicuri.out");

int n;
vector< pair<int, int> > v;
int aux[MAXN], len[MAXN];
int maxl;

int search(pair<int, int> x)
{
    int a = 0, b = maxl, mdl;
    while (a <= b) {
        mdl = a + ((b - a) >> 1);
        if (v[aux[mdl]].first < x.first && v[aux[mdl]].second < x.second &&
            v[aux[mdl + 1]].first >= x.first && v[aux[mdl + 1]].second >= x.second) {
                return mdl;
        } else if (v[aux[mdl + 1]].first < x.first && v[aux[mdl + a]].second < x.second) {
            a = mdl + 1;
        } else {
            b = mdl - 1;
        }
    }
    return maxl;
}

inline bool cmp(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) {
        return a.second > b.second;
    }
    return a.first < b.first;
}

int main() {
    f >> n;
    v.resize(n + 1);
    for (int i = 1; i <= n; i++) {
        f >> v[i].first >> v[i].second;
        if (v[i].first < v[i].second) {
            swap(v[i].first, v[i].second);
        }
    }

    sort(v.begin() + 1, v.end(), cmp);

    maxl = 1;
    aux[1] = 1;
    aux[0] = 0;
    len[1] = 1;

    for (int i = 2; i <= n; ++i) {
        int pos = search(v[i]);
        len[i] = pos + 1;
        aux[pos + 1] = i;

        maxl = max(maxl, pos + 1);
    }

    for (int i = 1; i <= n; i++) {
        cout << v[i].first << ' ';
    }
    cout << endl;
    for (int i = 1; i <= n; i++) {
        cout << v[i].second << ' ';
    }
    cout << endl << endl;
    for (int i = 1; i <= n; i++) {
        cout << len[i] << ' ';
    }
    cout << endl;
    for (int i = 1; i <= n; i++) {
        cout << aux[i] << ' ';
    }
    cout << endl;

    g << maxl << endl;

    return 0;
}