您的位置:首页 > 其它

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

2016-03-07 22:43 357 查看
原题: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: