您的位置:首页 > 其它

hdu 1025 Constructing Roads In JGShining's Kingdom LIS最长上升序列

2017-09-07 17:57 525 查看
传送门http://acm.hdu.edu.cn/showproblem.php?pid=1025

裸LIS, 不多说 (nlogn)

binary_sch 二分查找

road == 1 的时候没有复数 ‘s’.

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX = 5e5+5;
const int INF = 1 << 32 - 1;
int ary[MAX], dp[MAX], a, b, n, T;
int binary_sch(int l, int r, int num) {
if (r - l == 1) return l;
int tmp = (l+r) >>1; //这里被学长骂了, 原来写的是 (l+r)/2
if (dp[tmp] == num) return tmp - 1;
if (dp[tmp] > num) return binary_sch(l, tmp, num);
return binary_sch(tmp, r, num);
}
void getin() {
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
ary[a] = b, dp[i] = INF;
}
dp[0] = 0, dp
= INF, dp[n+1] = INF;
}
void judge() {
for (int i = 1; i <= n; ++i) {
int tmp = binary_sch(0, n, ary[i]);
dp[tmp+1] = ary[i];
}
int ans = 0;
while (dp[ans++] != INF) {;}
printf ("Case %d:\n", T);
if(ans-2 == 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", ans-2);
return ;
}
int main() {
for (T = 1; scanf("%d", &n) == 1; ++T) {
getin();
judge();
}
return 0;
}


2017-09-07
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐