ACdream 1216 Beautiful People(二维上升子序列 O(nlogn))

Beautiful People

Special Judge  Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
Submit  Statistic  Next Problem

Problem Description

      The most prestigious sports club in one city has exactly N members. Each of its members is strong and beautiful. More precisely, i-th member of this club (members being numbered by the time they entered the club) has strength Si and beauty Bi. Since this is a very prestigious club, its members are very rich and therefore extraordinary people, so they often extremely hate each other. Strictly speaking, i-th member of the club Mr X hates j-th member of the club Mr Y if Si <= Sj and Bi >= Bj or if Si >= Sj and Bi <= Bj (if both properties of Mr X are greater then corresponding properties of Mr Y, he doesn't even notice him, on the other hand, if both of his properties are less, he respects Mr Y very much).

      To celebrate a new 2003 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club prestige at the apropriate level, administration wants to invite as many people as possible.

      Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.

Input

      The first line of the input file contains integer N — the number of members of the club. ( 2 ≤ N ≤ 100 000). Next N lines contain two numbers each — S i and B respectively ( 1 ≤ Si, Bi ≤ 109).

Output

      On the first line of the output file print the maximum number of the people that can be invited to the party. On the second line output N integers — numbers of members to be invited in arbitrary order. If several solutions exist, output any one.

Sample Input

4
1 1
1 2
2 1
2 2

Sample Output

2
1 4
思考:不可以等于的时候,第一个升序,第二个降序,可以相等的时候,两个都是升序。(请自我思考,非常简单)
C++:

#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define S_queue<P> priority_queue<P, vector<P>,greater<P> >


typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
    int d = p < q ? 1 : -1;
    while(p != q) {
        cout << *p;
        p += d;
        if(p != q) cout << Gap;
    }
    cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 1e5;
const int MAXN = 1e5 + 5;
int n, dp[MAXN], ID[MAXN];
int Z[MAXN];

struct rect{
    int l, r, id;
    bool operator < (const rect & object) const{
        if(l == object.l) return r > object.r;
        return l < object.l;
    }
}rs[MAXN];

int main(){
    while(cin >> n){
        for(int i = 1;i <= n;i ++){
            cin >>rs[i].l >> rs[i].r;
            rs[i].id = i;
        }
        sort(rs + 1, rs + n + 1);
        fillchar(dp, 0x3f);
        fillchar(ID, 0);
        int ans = 0;
        for(int i = 1;i <= n;i ++){
            int tmp = lower_bound(dp + 1, dp + n + 1, rs[i].r) - dp ;
                dp[tmp] = rs[i].r;
                ID[i] = tmp;
                ans = max(ans, tmp);
        }
        cout << ans << endl;
        for(int i = n;i > 0;i --){
            if(ID[i] == ans){
                cout << rs[i].id;
                if(ans != 1) cout << " ";
                ans --;
            }
        }
        cout << endl;
    }
    return 0;
}


java:

import java.util.*;
import java.math.*;
import java.io.*;

public class Main{
	static final int MAXN = 100000 + 5;
	static int [] dp = new int[MAXN];
	static int [] ID = new int[MAXN];
	static final int INF = 0x3f3f3f3f;
	static class point implements Comparable<point> {
		public int l, r, id;
		@Override
		public int compareTo(point o) {
			// TODO Auto-generated method stub
			if (this.l == o.l) return o.r - this.r;
			return this.l - o.l;
		}
	}
	static point [] rs = new point[MAXN];
	public static int BS(int l, int r, int x){
		while(r >= l){
			int mid = (r + l) >> 1;
			if(dp[mid] == x) return mid;
			else if(dp[mid] > x) r = mid - 1;
			else l = mid + 1;
		}
		return l;
	}
	public static void main(String [] agrv)
	throws IOException
	{
		//System.setIn(new FileInputStream(new File("D:" + File.separator + "imput.txt")));
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext()){
			int n = cin.nextInt();
			for(int i = 1;i <= n;i ++){
				point s = new point();
				s.l = cin.nextInt();
				s.r = cin.nextInt();
				s.id = i;
				rs[i] = s;
			}
			Arrays.fill(dp, INF);
			Arrays.sort(rs, 1, n + 1);
			int ans = 0,len = 1;
			for(int i = 1;i <= n;i ++){
				int tmp = BS(1, len, rs[i].r);
				if(tmp == len){
					len ++;
				}
				dp[tmp] = rs[i].r;
				ID[i] = tmp;
				ans = Math.max(tmp, ans);
			}
			System.out.println(ans);
			for(int i = n;i > 0;i --){
				if(ID[i] == ans){
					System.out.print(rs[i].id);
					if(ans != 1) System.out.print(" ");
					ans --;
				}
			}
			System.out.println();
		}
	}
}


你可能感兴趣的:(ACdream 1216 Beautiful People(二维上升子序列 O(nlogn)))