SGU 199 Beautiful People(DP+二分)

时间限制:0.25s

空间限制:4M

题意:

有n个人,每个人有两个能力值,只有一个人的两个能力都小于另一个的能力值,这两个人才能共存,求能同时共存的最大人数。



Solution:

显然这是一个两个关键字的最长上升序列。

先按照第一种能力值为第一关键字从小到大,第二能力值为第二关键字从大到小排序(为什么?)

然后就是直接用O(nlogn)的DP就好了

(排序的方法是根据O(nlogn)DP算法中所用到的辅助数组的性质。。。如果对这个算法理解足够的话应该比较容易想到)

code(要用C++提交)

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
int n;
struct node {
    int first, second, id;
} f[100009];
int s[100009], pre[100009];
bool cmp (node a, node b) {
    if (a.first == b.first) return a.second > b.second;
    return a.first < b.first;
}
int main() {
    ios::sync_with_stdio (0);
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> f[i].first >> f[i].second, f[i].id = i;

    sort (f + 1, f + 1 + n, cmp);

    int ans = 0, t = 1;
    for (int t = 1; t <= n; t++) {
        int l = 0, r = ans, last = -1;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (f[t].second > f[s[mid]].second) {
                last = mid + 1;
                l = mid + 1;
            }
            else
                r = mid - 1;
        }
        pre[t] = s[last - 1];
        if (f[t].second < f[s[last]].second || s[last] == 0)
            s[last] = t;
        if (last > ans) ans++;
    }
    cout << ans << endl;
    for (int i = s[ans]; i; i = pre[i])
        cout << f[i].id << ‘ ‘;
}

时间: 2024-07-30 05:58:56

SGU 199 Beautiful People(DP+二分)的相关文章

SGU 199 Beautiful People 二维最长递增子序列

题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2)的算法很好想,不过这里会t掉,只能O(nlogn) 于是用二分来维护: 先把所有的数按x递增排序,x相同的按y递减排序(这里之所以要按y递减排序是因为为了写代码方便,递减的话你后面基本就只要考虑y的大小,如果不递减,你还要考虑x的大小的,具体的可以自己思考一下) 排完序之后我们接下来就只考虑y的大小

SGU 199. Beautiful People 二维LIS

第一维排序 第二维LIS #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int dp[100010]; int p[100010]; struct node { int x, y, id; }a[100010]; bool cmp(node a, node b) { if(a.x != b.x) return a.x < b.x; return a.y

SGU 199 - Beautiful People 最长上升子序列LIS

要邀请n个人参加party,每个人有力量值strength Si和魅力值 beauty Bi,如果存在两人S i ≤ S j and B i ≥ B j 或者  S i ≥ S j and B i ≤ B j 他们两个会产生冲突,问在不产生冲突的条件下,最多能邀请到几个人? [LIS]一开始将所有人按照Si升序排序,Si相同的按照Bi值降序排列,在这个基础上答案就是Bi的最长上升子序列的长度. 为什么Si相同时按照Bi值降序排列? 由求出的子序列时严格递增序列,如果对于相同的Si,Bi是递增,那

POJ-2533最长上升子序列(DP+二分)(优化版)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 Problem Description In the battlefield , an effective way to defeat enemies is to break their communication system. The information department told you that there are n enemy soldiers and their network w

HDU 3433 (DP + 二分) A Task Process

题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i][j]表示在t时间内前i个人完成j件A任务后所能完成B任务的最大数量. 代码中还有一些注释. 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using

[数位dp+二分] fzu 1074 Nancy&#39;s Birthday

题意:给m,n,问含有m个0的第k个数,是几位数,并且最高位是多少. 思路:和普通数位dp一样,加上个二分. 然后就是注意一下,极限值测试下能否算出来,这题极限值很大! 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #

poj3208 Apocalypse Someday 数位dp+二分 求第K(K &lt;= 5*107)个有连续3个6的数。

/** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. 思路:数位dp+二分. dp[i][j]表示长度为i,前缀状态为j时含有的个数. j=0表示含有前导0: j=1表示前缀连续1个6 j=2表示前缀连续2个6 j=3表示前缀连续3个6 j=4表示前缀不是6: */ //#include<bits/stdc++.h> #include<cstr

hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列,但数据太多.转化为求最长不下降子序列.太NB了.复杂度n*log(n). 解法:dp+二分 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <