HDU - 1160 FatMouse's Speed(最长不下降子序列)

题目链接:点击查看

题目大意:给出许多二元组(W,S),最后要求输出最长的满足W严格递增,S严格递减的子序列长度,以及方案,输出任意一种即可

题目分析:看起来像二维偏序,其实对任意一维排序后求最长不下降子序列或最长不上升子序列即可,模板题,增加了个路径输出,在更新的时候保存一下即可,有点忘了板子了,贴一个一个月之前写的代码回顾回顾:

#include<iostream>
#include<cstdio> 
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<sstream>
#include<cmath>
using namespace std;

typedef long long LL;

const int inf=0x3f3f3f3f;

const int N=1e3+100;

struct Node
{
	int w,s;
	int pos;
}a[N];

bool cmp(Node a,Node b)
{
	if(a.s==b.s)
		return a.w<b.w;
	return a.s>b.s;
}

int d[N];

int c[N];

int main()
{
//	freopen("input.txt","r",stdin);
	int x,y;
	int cnt=1;
	while(scanf("%d%d",&x,&y)!=EOF)
	{
		a[cnt].w=x;
		a[cnt].pos=cnt;
		a[cnt++].s=y;
	}
	sort(a+1,a+1+cnt,cmp);
	int len=1;
	memset(d,0,sizeof(d));
	d[1]=a[1].w;
	c[1]=1;
	for(int i=2;i<cnt;i++)
	{
		if(a[i].w>d[len])
		{
			d[++len]=a[i].w;
			c[i]=len;
		}
		else
		{
			int j=upper_bound(d+1,d+1+len,a[i].w)-d;
			d[j]=a[i].w;
			c[i]=j;
		}
	}
	cout<<len<<endl;
	
	int j=len;
	stack<int>ss;
	for(int i=cnt-1;i>=1;i--)
	{
		if(c[i]==j)
		{
			ss.push(a[i].pos);
			j--;
		}
	}
	while(!ss.empty())
	{
		cout<<ss.top()<<endl;
		ss.pop();
	}
	
	
	
	
	
	
	
	
	
	
	
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frozen_Guardian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值