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[];
int s[], pre[];
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 ();
cin >> n;
for (int i = ; i <= n; i++) cin >> f[i].first >> f[i].second, f[i].id = i; sort (f + , f + + n, cmp); int ans = , t = ;
for (int t = ; t <= n; t++) {
int l = , r = ans, last = -;
while (l <= r) {
int mid = (l + r) >> ;
if (f[t].second > f[s[mid]].second) {
last = mid + ;
l = mid + ;
}
else
r = mid - ;
}
pre[t] = s[last - ];
if (f[t].second < f[s[last]].second || s[last] == )
s[last] = t;
if (last > ans) ans++;
}
cout << ans << endl;
for (int i = s[ans]; i; i = pre[i])
cout << f[i].id << ' ';
}
SGU 199 Beautiful People(DP+二分)的更多相关文章
- [SGU 199] Beautiful People
[SGU 199] Beautiful People The most prestigious sports club in one city has exactly N members. Each ...
- SGU 199 Beautiful People 二维最长递增子序列
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20885 题意: 求二维最长严格递增子序列. 题解: O(n^2) ...
- 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 ≤ ...
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 题目大意: 给一棵树,n个节点,每条边有个权值,从每个点i出发有个不经过自己走过的点的最远距离 ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- hdu2993之斜率dp+二分查找
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。
/** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...
- hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- CMD打开远程并使用空白密码远程登录
记录一下,在单位管理局域网机器时 写出的小程序: 应用场景:比如异地A的局域网内主机需要远程登录进入系统调试,而A电脑的Radmin之类的远程控制软件无效,就只能使用操作系统自带的远程桌面功能,而,异 ...
- leetcode 字符串分割对称
public class Solution { public List<List<String>> partition(String s) { int len=s.length ...
- 从spark架构中透视job
本博文的主要内容如下: 1.通过案例观察Spark架构 2.手动绘制Spark内部架构 3.Spark Job的逻辑视图解析 4.Spark Job的物理视图解析 1.通过案例观察Spark架构 sp ...
- flexpaper 背景色变化
1.mxml文件头部:添加 backgroundAlpha="0" <s:Application xmlns:fx="http://ns.adobe.com/mxm ...
- Hibernate配置文件详解
Hibernate配置方式 Hibernate给人的感受是灵活的,要达到同一个目的,我们可以使用几种不同的办法.就拿Hibernate配置来说,常用的有如下三种方式,任选其一. 在 hibernate ...
- HDU2699+Easy
简单题. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...
- Python读写文件需要注意的地方 2015-03-31 23:19 69人阅读 评论(0) 收藏
<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> ...
- TOJ3660家庭关系(并查集+hash+图的连通性)
家庭关系 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 总提交: 38 测试通过: 9 描述 给定若干家庭成员之间的关系 ...
- [Angular 2] Keynote: Lazy Routing -- NGCONF
So How to do lazy loading for router in Angular 2. The nomarl way to write a router in Angular 2: Yo ...
- [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...