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. JavaWeb学习之JSP常用标签、EL表达式的运算符、JSTL标签库(6)

    1.JSP常用标签 * 只要支持JSP文件,常用标签有可以直接使用 * 格式: jsp:xxxx * jsp:forward ,完成jsp页面的转发 * page属性:转发的地址 <% requ ...

  2. 【JAVA基本数据类型包装类】

    一.概述 JAVA中一共有8种数据类型,分别是byte short int long boolean float double  char,与此相对应的,有8个类与它们分别对应: byte Byte ...

  3. POJ3294 Life Forms(后缀数组)

    引用罗穗骞论文中的话: 将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,用和例3 同样的方法将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中 ...

  4. android 入门-工程属性介绍

    工程属性 (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854) (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA ...

  5. 在Salesforce中通过dataloadercliq调用data loader来批量处理数据

    上一篇文章讲到,通过data loader去批量处理数据,那么这篇文章将主要讲解在Salesforce中通过dataloadercliq调用data loader来批量处理数据. 1): CLIq文件 ...

  6. Build better apps: Windows 10 by 10 development series

    http://blogs.windows.com/buildingapps/2015/08/05/build-better-apps-windows-10-by-10-development-seri ...

  7. linux下vim配置以及一些常用的快捷键

    一些常用的vim编辑器快捷键: h」.「j」.「k」.「l」,分别控制光标左.下.上.右移一格. 按「ctrl」+「b」:屏幕往“后”移动一页. 按「ctrl」+「f」:屏幕往“前”移动一页. 按「c ...

  8. cocos2dx游戏开发——微信打飞机学习笔记(三)——WelcomeScene的搭建

    一.场景与层的关系: cocos2dx的框架可以说主要由导演,场景,层,精灵来构成: 1.其中导演,意如其名,就是操控整个游戏的一个单例,管理着整个游戏. 2.场景就像电影的一幕剧情,所以说,懂得如何 ...

  9. 【SSH】 之 Struts2环境搭建及简单应用开发

    在上一篇文章中,我们一起了解了一下struts2的工作机制原理,接下来让我们进行一下简单应用的开发 (一)配置环境 1.建立web项目 2.导入jar包 其中struts2中有很多jar包,我们不需要 ...

  10. 关于MFC OpenGL环境配置的一点总结

    复制include时要小心..看vs给你load哪一个..名字一样..东西可不一定一样哦 http://www.cppblog.com/wicbnu/archive/2010/09/30/128123 ...