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. HTML5学习之文档结构和语义(一)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. Extjs ComboBox 动态选中第一项

    有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...

  3. 【JAVA与DOM4J实现对XML文档的CRUD操作】

    一.简介 1.网上下载DOM4J 1.6.1压缩包,解压开之后,发现几个目录和一个jar文件,jar文件是必须的文件其它目录: docs目录:帮助文档的目录,单击index.html: Quick s ...

  4. 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))

    sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  给定一个有向图 ...

  5. python多进程程序之间交换数据的两种办法--Queue和Pipe

    合在一起作的测试. #!/usr/bin/env python # -*- coding: utf-8 -*- import multiprocessing import random import ...

  6. 【20140113-2】MyEclipse生成javadoc时出错:编码GBK的不可映射字符

    今天生成java doc文档时,出现了如下所示的错误: 正在装入软件包 com.wisdom.test 的源文件...F:\workspace\StringUtils\src\com\wisdom\t ...

  7. 攻城狮在路上(叁)Linux(十六)--- 命令与文件的查找

    一.脚本文件的查询: 1.命令格式:which [-a] command; <==通过PATH来查找. -a:列出所有的,而不是仅列出第一个. 示例: which ifconfig; 注意:由于 ...

  8. JavaScript - BOM

    window 对象 window 对象是BOM的核心对象,也是ECMAScript规定的Global对象. 无法跨浏览精确获得窗口左边和上边的精确值,同样也无法确定浏览器窗口本身的大小,但是可以取得页 ...

  9. android 入门-git之上传本地代码到github

    github部分: 1.首先去github网站 上注册一个用户 2.说明 https://guides.github.com/activities/hello-world/ 2.点击 New repo ...

  10. Win10 VS2015 社区版切换到VS2013社区版 进行维护之前的项目

    前提:当先在Win10 OS 安装了vs2015之后开发Win UAP,之后要维护之前的WP8 版本,安装了VS2013社区版 打开后 1问.Exception from HRESULT: 0x897 ...