HDU 1025 —— Constructing Roads In JGShining's Kingdom 最长上升子序列

原题:http://acm.hdu.edu.cn/showproblem.php?pid=1025

题意:有n个穷城市,n个富城市,每个穷城市都要从某个富城市运输一种物资(穷城市和富城市的物资供需一对一),需要建立道路,但任意两条路不能交叉;穷城市和富城市分    别位列平行线两侧(均按1 - n 分布);给出城市个数n,下面n行输入两个数字 p 和 r 表示穷城市p要从富城市r运输物资,即需要在p和r之间建路,问最多可以建几条路;


思路:按p的顺序存入相对应的r,然后对r求最长上升子序列的个数;


#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 500000+10;
int n, cas = 0;
int a[maxn], stack[maxn];

int main()
{
	while(~scanf("%d", &n))
	{
		for(int i = 1;i<=n;i++)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			a[u] = v;
		}
		int top = 0;
		stack[top] = -1;
		for(int i = 1;i<=n;i++)
		{
			if(a[i] > stack[top])
				stack[++top] = a[i];
			else
			{
				int pos = lower_bound(stack+1, stack+top+1, a[i]) - stack;
				stack[pos] = a[i];
			}
		}
		printf("Case %d:\n", ++cas);
		if(top == 1)
		printf("My king, at most 1 road can be built.\n\n");
		else
		printf("My king, at most %d roads can be built.\n\n", top);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值