ZOJ 2319 Beautiful People(LIS二分法+路径输出)

求LIS的两种方法:DP 与 二分法 ~~ LIS(Longest Increasing Subsequence):最长上升子序列 这里分两种情况来看: 子序列严格递增(即子序列中不能存在相等) 子序列非降(即子序列中可以存在相等) 对于序列:a[1] , a[2] , a[3] , … , a[N] 一、动态规划(DP)   时间复杂度:O(N2) 子序列严格递增:dp[i] = max{1 , dp[j]+1}   ( j&lt... 阅读详情

Beautiful People

Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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 Si and beauty Bi. 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 Si <= Sj and Bi >= Bj or if Si >= Sj and Bi <= Bj (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 2005 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 prestige 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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


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 - Si and Bi respectively (1 <= Si, Bi <= 109).


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 Input

1

4
1 1
1 2
2 1
2 2


Sample Output

2
1 4



每一个点有两个属性 s,b;当一个的s,b都大于另外一个的时候才不会有矛盾,现在求没有矛盾的最大个数,并且输出这些数的序号(序号可以是乱的)


这个题有两个难点,第一个就是这么把它转化成一般的LIS,我看别的代码,先把s升序,如果s相同,按b降序,这样每次seach函数找到的f[i].b,的位置就是以这一个点为结尾的最长长度,就可以转化成一般的LIS了


还有一个难点就是保存路径,这个看别人博客居然是简单的mark[i]记下以i为结尾的最长上升字串的长度就可以了,真的是长见识了,换而言之 如果数据

3

1 1

2 2

2 3

 那么排序后就是1 3 2 (这是原来的序号)

mark[3]=2,mark[2]=2;mark[1]=1;//排序后的标号


这时候认真看我的print()函数想想吧


代码:



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
using namespace std;
#define N 100005

int dp[N],mark[N];

struct stud{
int s,b;
int pos;
}f[N];

int n,len;

int cmp(stud x,stud y)
{
	if(x.s==y.s)
		return x.b>y.b;
	return x.s<y.s;
}

int seach(int len,int x)
{
	int le,ri,mid,ans;
	le=0;ri=len;
	while(le<=ri)
	{
	  mid=(le+ri)>>1;
	  if(dp[mid]<x)
	  {
	  	ans=mid;
	  	le=mid+1;
	  }
       else
		ri=mid-1;
	}
	return ans+1; //ans是找到的不大于x的坐标,应该返回比它大一的坐标
}

void print()
{
	int j=0;
	printf("%d\n",len);

	for(int i=n;i>=1;i--)
	{
		if(mark[i]==len)  //d对于s相同的有可能有几个的mark相等,但是输出一个后len--了,所以只会输出一个
		{
			if(j) printf(" ");
			j=1;

			printf("%d",f[i].pos);
			len--;
		}
	}
   printf("\n");
}

int main()
{
	int i,j,t;
	scanf("%d",&t);
	while(t--)
	{
	    scanf("%d",&n);
	    for(i=1;i<=n;i++)
		{
			scanf("%d%d",&f[i].s,&f[i].b);
			f[i].pos=i;
		}
		sort(f+1,f+n+1,cmp);
		len=1;
		dp[1]=f[1].b;
		mark[1]=1;

		for(i=2;i<=n;i++)
		{
			j=seach(len,f[i].b);
			dp[j]=f[i].b;
			mark[i]=j;
			if(j>len)
			      len=j;
		}

		print();
	}
	       return 0;
}




二分法求LIS并记录路径 二分法求LIS一般是二分维护一个数组d[len]d[len]d[len],lenlenlen表示最大长度,但是数组ddd中存储的并不是LIS(因为入的时候前后顺序被打乱),如果要得到LIS,还需记录路径。 再创建一个数组pos[i]pos[i]pos[i],表示第 ... 阅读详情

相关推荐

我用 AI 做了个开源地图工具:从 OpenStreetMap 一键获取全国 AOI/POI 数据

本文介绍了一款免费开源的OSM数据查询与可视化工具,旨在解决Overpass QL语法晦涩、数据获取门槛高的问题。工具支持面数据(AOI)、点数据(POI)及路网三种查询模式,内置30+常用模板实现一键检索。具备高德与OSM双底图切换、坐标自动转换功能,支持GeoJSON、SHP、CSV多格式导出,同时保留自定义查询能力。作为纯HTML单文件应用,可零成本部署至GitHub Pages在线使用。该工具有效降低了地理数据获取难度,是替代商业地图API、高效提取全球开源地理信息的实用利器。

weixin_45812624的博客 585

LIS LCS n^2和nlogn解法 以及LCIS

首先介绍一下LIS和LCS的DP解法O(N^2) LCS:两个有序序列a和b,求他们公共子序列的最大长度 我们定义一个数组DP[i][j],表示的是a的前i项和b的前j项的最大公共子序列的长度,那么由于是用迭代法,所以计算DP[i][j]前,DP[i-1][j]和DP[i][j-1]就都已经计算出来了,不难理解就可以得出状态转移方程: DP[i][j] = DP[i-1][j-1] + 1...

weixin_33747129的博客 315

NRF24L01学习操作教程(二)——NRF实现一对一,一对多通讯

以上内容详细介绍了,使用NRF24L01实现1对1单向通讯、1对1双向通讯、1对多双向通讯。

Joseph Wen的博客 7388

动态规划(二分法改进):LIS问题以及应用题 (子序列型)

LIS问题解决思路,着手dp数组记录下长度为b的单调递增子序列最小结束元素。 Example: array = [2,1,4,3,8,7,5,6,9] 对每个元素array[i]进行单独判断,考虑array[i]可构成最长为多长的局部LIS。 局部的搜索采用二分,以dp数组为域,以array[i]为判定值,找到一个dp中的位置 j 满足: ( dp[j]<array[i] and ...

qq_42418728的博客 777

nsight-system教程

参考nsight-compute使用教程-优快云博客。

youzjuer的博客 1893

Maya vray 3S皮肤材质球设置与材质节点连接

Maya vray 3S皮肤材质球设置与材质节点连接

WETQETF的博客 2697

数据结构与算法随笔之------动态规划问题之------最长上升子序列(LIS)问题(一文搞懂最长上升子序列(LIS))

LIS (最长上升子序列(计算机科学)) 定义:最长上升子序列(Longest Increasing Subsequence,LIS),在计算机科学上是指一个序列中最长的单调递增的子序列。 这里借鉴一个大佬的博客:https://www.cnblogs.com/GodA/p/5180560.html 方法1:枚举法(也就是动态规划) 我们都知道,动态规划的一个特点就是当前解可以由上一个阶...

markconca的博客 432

SAP-PLM传输MBOM传输至SAP

BOM 项目文本 (行 2)PLM创建及修改BOM接口。PLM 创建BOM接口抬头。BOM 项目文本(行1)PLM 创建BOM接口抬头。PLM BOM 错误消息。PLM BOM接口组件。组件数量 (BTCI)

qq_32198063的博客 431

【LIS】O(nlogn)二分法搜索+路径输出

看了很多博客,都只是介绍了如何去找最大的长度,而没有介绍

DoubleL的进阶史 2565

STM32实战开发(120):基于STM32的智能pH值检测系统开发

pH值检测:通过pH传感器实时检测溶液的酸碱度。数据采集与处理:STM32单片机通过模拟信号采集并进行数字化处理。数据显示与控制:通过LCD或OLED屏幕实时显示pH值。数据存储与上传:将数据保存在本地SD卡,或通过Wi-Fi模块上传至云平台。低功耗设计:适合长时间运行,减少能源消耗。本文介绍了如何基于STM32单片机开发智能pH值检测系统,涉及硬件设计、软件开发及应用实现。该系统可以实时检测pH值,并将数据展示、存储、上传至云平台,适用于环境监测、农业、水处理等多个领域。

2201_76125393的博客 1816

apollo-慢速车向左绕行

当主车前方出现其他车辆时,通过左侧车道对前车绕行。

是菜鸟o的博客 309

ZOJ 2319 Beautiful People (LIS的变形)

题目大意 有n个人的,每个人有一个si和bi,对于i和j来说,只有当si < sj && bi < bj ,或者 si > sj && bi > bj 时,二人才不会互相讨厌。 也就是说,只有两个人其中一方的s和b都小于另一方时,二人才不会互相讨厌。 求最多有多少人不会互相讨厌 分析 给n个人按s排序从小到大,若s相等则按b从大到小排序。 然后求b的最长上升子序列的长度ans即可 用O(nlogn)

既不是天才,便做那疯子! 483

ModuleNotFoundError: No module named‘langchain.schema.output parserlangchain.schema is not a package

电脑里现有的langchain库版本与import所需的不同,卸载原有langchain,重新下载最新的langchain。在步骤1后,若仍不可运行。在确保已重新下载新langchain后,重启python后,可正常运行。在版本号中填入所需的langchain版本号,可在requirements.txt查看。运行如下代码时,出现error。

weixin_45751925的博客 1713

ZOJ 2319 Beautiful People(最长单调递增子序列)(二维LIS)

Beautiful PeopleThe 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

Darkarts 7703

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

LIS+二分法

http://poj.org/problem?id=3903 数列里是存从小到大排的数,二分也是为了这个服务的,不断更新。而len才是所求长度 1 #include <iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #inc...

weixin_30699463的博客 191

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LIS 最长上升子序列 (n*logn) 模板 (二分查找+递归)

最长上升子序列是很早就接触了的问题了,一直用的是动态规划n*n的方法,也知道那不是最好的,可以优化,今天看博客无意中看到LIS,LCS两个词,就特意找了博客看了看,主要是理解一下这里的思想,其实蛮复杂难懂的,自己很难说清楚,还是得引用人家的博客才行。点击打开链接 下面还有模板: #include #include using namespace std; int a[1000]; in

konghhhhh的博客 788

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LIS 的 n*log 算法 ———二分维护

upper_bound只会替换让留在数组里的最长递增子序列尽可能小 由于upper查出来的结果下标是那个可以插入这个值得最后一个元素 所以他会把大的数变成小的数upper将可以替换成新元素的元素替换成更小的 将不能替换的元素 大的元素直接放在最后面 很神奇你会发现 这个问题 通过一个二分就能解决 如何解决的呢 我们要在vector中维护一个递增序列 如何维护 那就是每当我们遍历过程

qq_33859479的博客 820

hdu 1025 二分法求LIS

题目给出两个序列,两个序列间有边相连,我们要选出不交叉的边,使得所选的边最多      我们发现,如果我们对其中一个序列从小到大排序的话,这个问题就变成了求另一个序列的最长上升子序列的问题。      由于这个题目的数据比较大,一般的求最长上升子序列的方法是n方的,显然不能满足题目的要求,我们建立一个数组g,g[i]表示长度为i的上升子序列最后一个数的最小值,那么显然g数组是严格单调的,确切的

不知 609

zoj 3908 Number Game(zoj 2015年10月月赛F题)

Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored Bob is playing a number game. In the beginning, there are n numbers. For each turn, Bob will take out two numbers from the

ACway的专栏 5069

ZOJ3953 Intervals 【贪心 双线程活动分配问题】

Intervals Time Limit: 1 Second Memory Limit: 65536 KB Special Judge Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that there does not exist th

LuRiCheng的博客 939

LIS的三种求解方法

1.  O(n^2) 传统的求解方法 ,思路为dp,状态转移方程为 dp[i]=max( dp[j]+1,1)  即到目前的i为止,对前面出现的a[j](ja[j]的情况 ,就使用状态转移方程。 转移方程代表了两种可能 ,第一种为第i个元素自己成为一个上升的队列  ,或者是由于前面的a[j] dp[j]的基础之上形成了dp[i] = dp[j]+1  但前提是a[i]>a[j] # in

xws117的专栏 3090

HDU - 4352 XHXJ's LIS(数位DP,二分法求LIS)

题目链接:HDU - 4352 XHXJ’s LIS Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of...

Plus Ultra! 279

zoj 3160(区间dp 简单)

题目链接:点击打开链接 题目大意:一些人编号为(0~n-1),有些关系在某相邻两人中间,若有关系则就离开这个队列;求离开人数最多的是?                     连连看的即视感,仔细分析为区间dp #include #include #include #include int dp[305][305],queue[305],map[305][305];//MAP用于存储关系,q

阿歇的专栏 787

关于LIS(普通方式及二分查找方式)

一:关于LIS: LIS及最长上升子序列,就是对于一个长度为n的数组,上升子序列指的是对于数组中任意的i<j都满足a[i]<a[j]的子序列。 例子: 对于固定的数组,虽然LIS序列不一定唯一,但LIS的长度是唯一的。例如序列2,1,5,3,6,4,6,3的最长上升子序列为1,3,4,6和2,3,4,6 长度均为4 先确定动态规划的状态,这个问题可以用序列某一项作为结尾来作为一个状态。...

lsd&xql的博客 1132

ZOJ seeking 【深搜问题】及深搜的理解

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There

New soul 903

ZOJ-3203-Light Bulb【6th浙江省赛】【数学公式】【三分】

ZOJ-3203-Light Bulb

Linoy 1359
上一篇: POJ 1631 Bridging signals(LIS 二分 快速方法)
下一篇: HDU 1686 Oulipo(kmp)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值