hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
来源:互联网 发布:知乎 王西安 编辑:程序博客网 时间:2024/05/16 07:15
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
解题报告:刚开始想用二分匹配,但是二分匹配可以使线相交,明显不行;
其实这道题仔细挖掘,可以发现很多信息 !每个P都与唯一的一个r相连,每一个r都与唯一的p相连,就保证从1 到n的每个数都不会空着,就可以从1-----n来找富城市所对应的穷城市,穷城市之间的序号也从小到大即可加入dp[]栈,这里数据500000,至多nlog(n)的算法,如何从小到大,容易想到最长上升子序列
code:
#include<cstdio>//#include<cstring>#include<algorithm>using namespace std;const int maxn = 500000 + 10;int a[maxn],dp[maxn];int LIS(int n){int sta,end,mid,len;end = len = 1;dp[1] = a[1];for(int i = 2; i <= n; i++){sta=1;end = len;while(sta <= end){mid=(sta + end)/2;if(dp[mid] > a[i])end = mid-1;elsesta = mid+1;}dp[sta] = a[i];if(sta > len)len = sta;}return len;}int main(){int n,x,y,ncase=0;while(scanf("%d", &n) != EOF){for(int i = 0; i<n; i++){scanf("%d %d",&x, &y);a[x]=y;}//sort(a, a+n);int ans = LIS(n);printf("Case %d:\n",++ncase);if(ans==1)printf("My king, at most %d road can be built.\n\n", ans);else printf("My king, at most %d roads can be built.\n\n",ans);}return 0;}
- Constructing Roads In JGShining's Kingdom(最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列+n*logn算法)
- hdu 1025 Constructing Roads In JGShining's Kingdom(树状数组求最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列(nlogn)
- HDU-1025 Constructing Roads In JGShining's Kingdom O(nlogn)的最长上升子序列
- hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
- 【DP】HDU 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- 最长上升子序列 nlogn算法 hdu 1025 Constructing Roads In JGShining's Kingdom
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列nlogn算法)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- HDU 1025 —— Constructing Roads In JGShining's Kingdom 最长上升子序列
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS最长上升子序列)
- 【HDU 1025】Constructing Roads In JGShining's Kingdom(最长上升子序列LIS)
- HDU 1025 A - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- HDU 1025 - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- 网络状态的改变及判断
- linux中动态链接库的创建与使用
- 共享
- String与InputStream转换
- signal 详解
- hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
- Java 反射 方法调用
- Bloom Filter算法
- FFmpeg和SDL的API
- 非递归遍历二叉树
- SQL server2008 收缩数据库日志文件
- 页面缓存概述
- C#数组
- ABAP OO静态和实例方法区别