Constructing Roads In JGShining's Kingdom
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 500005
int n;
int bakup[N];
struct node {
int p;
int r;
} a[N];
int dp[N];
int cmp(node b, node c) {
return b.p < c.p;
}
void LIS() {
//dp[i]表示前i个数字中加上第i个数字后,有几个数字是递增的,即前i个数字中有多少个比第i个数字小的+1
//int bakup[N];
memset(bakup, 0, sizeof(bakup));
memset(dp, 0, sizeof(dp));
int top = 0;
bakup[top] = 0xbfbfbfbf;
for(int i = 1; i <= n; i++) {
//如果a[i]>栈顶部元素,则压栈
if(a[i].r > bakup[top]) {
bakup[++top] = a[i].r;
dp[i] = top;
}
//如果a[i]不大于栈顶部元素,则二分查找第一个比a[i]大的元素
else {
int l = 1, r = top;
while(l <= r) {
int mid = (l+r) >> 1;
if(a[i].r > bakup[mid]) {
l = mid + 1;
} else
r = mid - 1;
}
//替换a[i]
bakup[l] = a[i].r;
dp[i] = l;
}
}
}
int main() {
//freopen("data.in", "r", stdin);
int cas = 0;
while(scanf("%d", &n) != EOF) {
cas++;
memset(a, 0, sizeof(a));
for(int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].p, &a[i].r);
}
sort(a+1, a+n+1, cmp);
LIS();
int maxx = 0xbfbfbfbf;
for(int i = 1; i <= n; i++) {
if(dp[i] > maxx)
maxx = dp[i];
}
if(maxx == 1) {
printf("Case %d:\n", cas);
printf("My king, at most %d road can be built.\n\n",maxx);
} else {
printf("Case %d:\n", cas);
printf("My king, at most %d roads can be built.\n\n",maxx);
}
}
return 0;
}