HDU 1025 Constructing Roads In JGShining's Kingdom (简单dp, 最长上升子序列LIS)
2017-09-28 15:10
507 查看
题目链接 : Constructing Roads In JGShining’s Kingdom
题意: 有rich city和poor city 两种,分别有n个,编号均为1-n, 一个poor city只会与一个rich city连接,两种city的布局(连边不允许交叉如下
从左到右,均为编号为1-n.
给定边(poor city 连接 rich city),问在不相交的情况下能连接几条边.
解法:
在poor city有序的情况下,只要求rich最长上升子序列即可(nlogn求解)
题意: 有rich city和poor city 两种,分别有n个,编号均为1-n, 一个poor city只会与一个rich city连接,两种city的布局(连边不允许交叉如下
从左到右,均为编号为1-n.
给定边(poor city 连接 rich city),问在不相交的情况下能连接几条边.
解法:
在poor city有序的情况下,只要求rich最长上升子序列即可(nlogn求解)
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a; i<=b; ++i) #define mp make_pair #define pb push_back #define ms(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int>vi; const int maxn = 5e5+7; int n; struct node{ int a, b; }p[maxn]; bool cmp(node a, node b){ if(a.a != b.a) return a.a<b.a; return a.b < b.b; } int List[maxn]; int main(){ // freopen("in.txt", "r", stdin); int cas=0; while(~scanf("%d", &n)){ printf("Case %d:\n", ++cas); rep(i, 1, n) scanf("%d %d", &p[i].a, &p[i].b); sort(p+1, p+1+n, cmp); int ans=0; int cnt=1; List[0] = p[1].b; rep(i, 2, n){ int inx = lower_bound(List, List+cnt, p[i].b)-List; List[inx] = p[i].b; if(inx == cnt) cnt++; } if(cnt==1) printf("My king, at most %d road can be built.\n", cnt); else printf("My king, at most %d roads can be built.\n", cnt); printf("\n"); } return 0; }
相关文章推荐
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- 【DP】HDU 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列
- 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(最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom 深夜又一波DP,最长上升子序列(O(nlogn)算法)!尼玛坑爹的输出啊!!
- 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 LIS最长上升序列
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列(nlogn)
- hdu 1025 Constructing Roads In JGShining's Kingdom--最长上升子串(时间优化)
- HDOJ 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
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- 暑期dp46道(16)hdoj 1025 Constructing Roads In JGShining's Kingdom最长上升子序列(二分)
- HDU 1025.Constructing Roads In JGShining's Kingdom【最长上升子序列n×logn算法】【1月6】
- 【DP|LIS】HDU-1025 Constructing Roads In JGShining's Kingdom
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)