HDU 1160-FatMouse's Speed-最长下降子序列-两种求法

9 篇文章 0 订阅
7 篇文章 0 订阅

FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

Input

Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.

Output

Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],…, m[n] then it must be the case that

W[m[1]] < W[m[2]] < … < W[m[n]]

and

S[m[1]] > S[m[2]] > … > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900

Sample Output

4
4
5
9
7

题目大意:

给定若干只老鼠信息:重量和速度。
选出尽可能多的老鼠满足重量越大,速度越慢的规律。

核心思想:

按重量升序排序,求最长下降子序列,记录并输出路径
最长下降子序列最长上升子序列的求法如出一辙。
实现方式有多种,这里采用了两种方式:
1、贪心+二分
2、线段树
详见代码。

贪心+二分:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1010;
//记录路径 
int ans[N]; 
//存储每个老鼠的信息 
//id为老鼠原位置
//f是下降子序列中h[i]的前一个元素,用来输出时寻路的 
struct node{
	int id,w,s,f; 
}h[N];
//zh存储每个下降子序列的实时末尾
//x是zh[i].s在排好序的h数组中的位置 
struct no{
	int x,s;
}zh[N];
bool cmp(node a,node b)
{
	if(a.w!=b.w)
		return a.w<b.w;
	return a.s<b.s;
}
int main()
{
	int w,s,cnt=0;
	//输入 
	while(scanf("%d%d",&w,&s)!=EOF)
	{
		cnt++;
		h[cnt].id=cnt;
		h[cnt].w=w;
		h[cnt].s=s;
	}
	//排序 
	sort(h+1,h+cnt+1,cmp);
	//zh数组初始化 
	zh[0].x=0;
	zh[0].s=10010;
	int end=1;
	//开始遍历排好序的老鼠信息 
	for(int i=1;i<=cnt;i++)
	{
		//二分从zh数组中找第一个小于h[i].s的数 
		int l=0,r=end-1,flag=0;
		//当二分区间长度小于3的时候就跳出,防止死循环
		while(r-l>1) 
		{
			int mid=(l+r)>>1;
			if(zh[mid].s>h[i].s)
				l=mid+1;
			else if(zh[mid].s<h[i].s)
				r=mid;
			else
			{
				flag=1;
				break;
			}
		}
		//zh数组中已经存在h[i].s,则直接跳过 
		if(flag||zh[l].s==h[i].s||zh[r].s==h[i].s)
			continue;
		//确定h[i].s要替换的位置 
		int p;
		if(zh[l].s<h[i].s)
			p=l;
		else if(zh[r].s<h[i].s)
			p=r;
		//若zh元素都比h[i].s大,则放在末尾,zh长度+1 
		else 
			p=end++;
		//zh数组更新 
		zh[p].s=h[i].s;
		zh[p].x=i;
		//路径更新 
		h[i].f=zh[p-1].x;
	}
	//输出 
	printf("%d\n",end-1);
	int sum=0,k=zh[end-1].x;
	while(k)//寻找路径 
	{
		ans[sum++]=h[k].id;
		k=h[k].f;
	}
	for(int i=sum-1;i>=0;i--)
		printf("%d\n",ans[i]);
	return 0;
}

线段树:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1010,M=1e4+10;
//记录路径 
int ans[N];
//存储每个老鼠的信息 
//id为老鼠原位置
//f是下降子序列中h[i]的前一个元素,用来输出时寻路的 
struct no{
	int id,w,s,f;
}h[N];
//线段树query函数的返回结构体 
struct inode{
	int x,d;
	inode()
	{
	}
	inode(int xx,int dd)
	{
		x=xx;
		d=dd;
	} 
};
//线段树结点 
struct node{
	int l,r;
	inode mx;
}tr[M<<2];
//sort排序比较函数 
bool cmp(no a,no b)
{
	if(a.w!=b.w)
		return a.w<b.w;
	return a.s<b.s;
}
//线段树回溯函数 
void pushup(int m)
{
	if(tr[m<<1].mx.d>tr[m<<1|1].mx.d)
	{
		tr[m].mx.d=tr[m<<1].mx.d;
		tr[m].mx.x=tr[m<<1].mx.x;
	}
	else
	{
		tr[m].mx.d=tr[m<<1|1].mx.d;
		tr[m].mx.x=tr[m<<1|1].mx.x;
	}
	return;
}
//线段树建立 
void build(int m,int l,int r)
{
	tr[m].l=l;
	tr[m].r=r;
	if(l==r)
	{
		tr[m].mx.d=0;
		tr[m].mx.x=0;
		return;
	}
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
	pushup(m);
	return;
}
//线段树更新 
void update(int m,int x,inode v)
{
	if(tr[m].l==x&&tr[m].r==x)
	{
		tr[m].mx.d=v.d;
		tr[m].mx.x=v.x;
		return;
	}
	int mid=(tr[m].l+tr[m].r)>>1;
	if(x<=mid)
		update(m<<1,x,v);
	else
		update(m<<1|1,x,v);
	pushup(m);
	return;
}
//线段树查询 
inode query(int m,int l,int r)
{
	if(tr[m].l==l&&tr[m].r==r)
		return tr[m].mx;
	int mid=(tr[m].l+tr[m].r)>>1;
	//查左边 
	if(r<=mid)
		return query(m<<1,l,r);
	//查右边 
	else if(l>mid)
		return query(m<<1|1,l,r);
	//查两边,择优返回 
	else
	{
		inode re1=query(m<<1,l,mid);
		inode re2=query(m<<1|1,mid+1,r);
		if(re1.d>re2.d)
			return re1;
		else
			return re2;
	}
}
int main()
{
	int w,s,cnt=0;
	//输入 
	while(scanf("%d%d",&w,&s)!=EOF)
	{
		cnt++;
		h[cnt].id=cnt;
		h[cnt].w=w;
		h[cnt].s=s;
	}
	//排序 
	sort(h+1,h+1+cnt,cmp);
	//建树 
	build(1,1,10000);
	//线段树 
	int md=0,p;
	//遍历h数组,求最长下降子序列 
	for(int i=1;i<=cnt;i++)
	{
		//线段树查询 
		inode re=query(1,h[i].s+1,10000);
		//最长的长度更新 
		if(re.d+1>md)
		{
			md=re.d+1;
			p=i;
		}
		//线段树更新 
		update(1,h[i].s,inode(i,re.d+1));
		//路径更新 
		h[i].f=re.x;
	}
	//输出 
	printf("%d\n",md);
	int sum=0;
	while(p!=0)//寻路 
	{
		ans[sum++]=h[p].id;
		p=h[p].f;
	}
	for(int i=sum-1;i>=0;i--)
		printf("%d\n",ans[i]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值