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. jQuery 操作复选框(checkbox) attr checked不起作用

    参考资料 http://www.paobuke.com/develop/javascript/pbk849.html   这天用到jQuery功能,想实现一个简单的复选框动态全选或全不选,结果测试发现 ...

  2. bootstrap的验证和确认对话框

      BootstrapValidator: http://bv.doc.javake.cn/api/   引用 <!-- jquery-confirm.确认对话框 --> <link ...

  3. Overview and Evaluation of Bluetooth Low Energy: An Emerging Low-Power Wireless Technology

    转自:http://www.mdpi.com/1424-8220/12/9/11734/htm Sensors 2012, 12(9), 11734-11753; doi:10.3390/s12091 ...

  4. PHP获取远程图片并调整图像大小(转)

    <?php /** * *函数:调整图片尺寸或生成缩略图 *修改:2013-2-15 *返回:True/False *参数: * $Image 需要调整的图片(含路径) * $Dw=450 调整 ...

  5. Delphi中怎么结束线程(这个线程是定时执行的)(方案二)

    上篇博客中提出了一个问题:怎么结束一个定时循环执行的线程,并给出了一个解决方案,但是又出现了一个问题,详细去参考上一篇博客. 然后出去撒了个尿,突然脑子里出现了一个想法(看来工作和思考久了,出去走走, ...

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

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

  7. C++ 基础 const放在函数末尾的意思

  8. 注解:【有连接表的】Hibernate单向N->1关联

    Person与Address关联:单向N->1,[有连接表的] Person.java package org.crazyit.app.domain; import javax.persiste ...

  9. android 瀑布流

    我们还是来看一款示例: 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此.就如我们的方角图形,斯通见惯后也就出现了圆角.下面我简单介绍下实现 ...

  10. FastDFS简介

    一.FastDFS概述: FastDFS是一个开源的轻量级分布式文件系统,他对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.下载)等,解决了大容量存储和负载均衡的问题,高度追求高性能 ...