zoukankan      html  css  js  c++  java
  • 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 << ' ';
    }
    View Code

                 

  • 相关阅读:
    innerHTML与appendChild(newnodeText)的区别
    转]解析C语言中的sizeof
    #pragma 预处理指令详解
    Linux目录结构和常用命令
    Linux下select, poll和epoll IO模型的详解
    重载,继承,重写和多态的区别:
    CentOs7实现lvs+nginx+keepalived负载均衡(DR模式)
    shell中的通配符(grep、cut、sort等)
    kali虚拟机 联网问题解决办法
    MongoDB 数据库
  • 原文地址:https://www.cnblogs.com/keam37/p/4016274.html
Copyright © 2011-2022 走看看