199. Beautiful People

time limit per test: 0.25 sec. 
memory limit per test: 65536 KB
input: standard 
output: standard
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 S i and beauty B i . 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 S i ≤ S j and B i ≥ B j or if S i ≥ S j and B i ≤ B j (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 presti≥ 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 i respectively ( 1 ≤ S i, B i ≤ 10 9 ). 
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 test(s)
Input

1 1 
1 2 
2 1 
2 2 
 
Output
 

1 4 
 
/*
最长上升子序列的变形。 要保证Si和Bi都有严格的递增,再求最长上升子序列的元素排序。(这个S和B给满分。。) 首先想到的是贪心,以两点都升序排序,排序后从最小的往后加,但是这样忽略了其他的更优解WA的理所当然。。 再后来想了下这题是个背包问题,用dp来记录以这个点结尾的前面的子序列长度,实现过程附图:
以此往下类推

最后得到了一组ans以各个元素结尾的最大的子序列长度

最后这样找出来的顺序就是整个过程最小子序列的顺序,当然一开始id被打乱了,不要忘了把id最后对应一下。 最后还有一个问题就是元素顺序的打印,这里一开始写的递归总有点问题,最后看了下别人的题解借鉴了点用ans标记顺序的思想。 */
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define memset(x,y) memset(x,y,sizeof(x))
using namespace std;
#define MX 100005 int dp[MX],ans[MX];
int Prt[MX];
struct node {
int s,b;
int id; //标记实际的输入的顺序
} rel[MX]; int n,len; int cmp(node a,node b){
if(a.s==b.s)
return a.b>b.b;
return a.s<b.s;
} int Query(int len,int x){
int l,r,mid,rt;
l=0;r=len; //用二分的方法往前找出到达这个点的前面的最大单调上升序列的最小的值
while(r>=l){
mid=(l+r)>>1;
if(dp[mid]<x){
rt=mid+1; //找到比x小的数的后一个位置
l=mid+1;
}
else r=mid-1;
}
return rt;
} int Print(){
int j=0;
for(int i=n;i>=1;i--){
if(ans[i]==len) {
Prt[j++]=rel[i].id; //保存每个数字最小的结果
len--;
}
}
return j;//返回长度
} int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
scanf("%d%d",&rel[i].s,&rel[i].b);
rel[i].id=i;
}
memset(ans,0);
memset(dp,0);
sort(rel+1,rel+n+1,cmp); //将得到的所有值按照 s从小到大,b的从大到小排序
len=1;
dp[1]=rel[1].b; //dp保存最各个长度的最长单调序列的最大的值
ans[1]=1;
int Q;
for(int i=2;i<=n;i++){
Q=Query(len,rel[i].b); //找到比rel[i].b小的最长单调子序列的下一个位置
ans[i]=Q; //ans[i]保存以这点为结尾的最长上升子序列的最大长度
dp[Q]=rel[i].b; //更新这个位置的最小值
len=len>Q?len:Q; //保存最大长度。
}
int flag=1;
printf("%d\n",len);
int lon=Print();
for(int i=lon-1;i>=0;i--){
if(flag)flag=0;
else printf(" ");
printf("%d",Prt[i]); //逆序输出。
}
puts("");
}
return 0;
}

  

ACM: 强化训练-Beautiful People-最长递增子序列变形-DP的更多相关文章

  1. [程序员代码面试指南]最长递增子序列(二分,DP)

    题目 例:arr=[2,1,5,3,6,4,8,9,7] ,最长递增子序列为1,3,4,8,9 题解 step1:找最长连续子序列长度 dp[]存以arr[i]结尾的情况下,arr[0..i]中的最长 ...

  2. 最长递增子序列问题 nyoj 17单调递增最长子序列 nyoj 79拦截导弹

    一,    最长递增子序列问题的描述 设L=<a1,a2,…,an>是n个不同的实数的序列,L的递增子序列是这样一个子序列Lin=<aK1,ak2,…,akm>,其中k1< ...

  3. hunnu 11313 无重复元素序列的最长公共子序列转化成最长递增子序列 求法及证明

    题目:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11313 湖师大的比赛,见我的另一篇水题题解,这里要说的 ...

  4. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)- 勤奋的杨老师(最长递增子序列)

    链接:https://www.nowcoder.com/acm/contest/116/C来源:牛客网 题目描述 杨老师认为他的学习能力曲线是一个拱形.勤奋的他根据时间的先后顺序罗列了一个学习清单,共 ...

  5. (转载)最长递增子序列 O(NlogN)算法

    原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...

  6. 最长公共子序列(LCS)和最长递增子序列(LIS)的求解

    一.最长公共子序列 经典的动态规划问题,大概的陈述如下: 给定两个序列a1,a2,a3,a4,a5,a6......和b1,b2,b3,b4,b5,b6.......,要求这样的序列使得c同时是这两个 ...

  7. 最长递增子序列 O(NlogN)算法

    转自:点击打开链接 最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS. 排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个 ...

  8. 51nod 1134 最长递增子序列

    题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...

  9. 动态规划 - 最长递增子序列(LIS)

    最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...

随机推荐

  1. javascript - DOM对象控制HTML元素详解

    1.方法   getElementsByName() -- 获取name getElementByTagName() -- 获取  getAttribute()         --获取元素属性 se ...

  2. DC/DC与LDO的差别

    转自:http://bbs.eetop.cn/thread-459121-1-1.html 在平时的学习中,我们都有接触LDO和DC/DC这一类的电源产品,但作为学生的我们队这些东西可能了解不够深刻, ...

  3. 几年前做家教写的C教程(之三专讲了递归和斐波那契)

    C语言学习宝典(3) 数组: 一维数组的定义: 类型说明符  数组名[常量表达式] 例如: int  a[10]; 说明:(1)数组名的命名规则和变量名相同,遵循标示符命名规则 (2)在定义数组时需要 ...

  4. 【翻译二十一】java-并发之分拆和合并

    Fork/Join This section was updated to reflect features and conventions of the upcoming Java SE 8 rel ...

  5. [LeetCode] Merge Sorted Array

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  6. phpcms 完美实现 导航栏当前栏目高亮

    我们在用phpcms做网站的时候,经常碰到导航栏高亮功能,或者侧栏高亮,这个会涉及到几个问题: .栏目列表页子栏目高亮判断,如果当前页面为子栏目,他的顶级栏目如果在导航栏也要高亮. .内容页高亮,这个 ...

  7. 11g添加asm

    1.创建组 2.创建grid用户 3.用grid安装Gride Infrastructure软件 4.执行root.sh[root@ora11g softdb]# /u01/app/11.2.0/gr ...

  8. 第十篇:扩展SOUI的控件及绘图对象(ISkinObj)

    尽管SOUI已经内置了大部分常用的控件,很显然内置控件很难满足各种应用的形式各异的需求. 因此只有提供足够的扩展性才能满足真实应用场景. 除了将系统尽可能的组件化外,SOUI在控件自绘(SWindow ...

  9. HTML5头部标签备忘

    DOCTYPE DOCTYPE(Document Type),该声明位于文档中最前面的位置,处于html 标签之前,此标签告知浏览器文档使用哪种HTML 或者XHTML 规范. 推荐使用HTML5 推 ...

  10. oc中定时器的基本使用

    // 时间间隔 调用的对象  调用的方法 用户信息 是否循环 [NSTimer scheduledTimerWithTimeInterval: target:self selector:@select ...