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;
}


發佈了60 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章