- 题目链接 : 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求解)
#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(){
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;
}