HDU 1025 Constructing Roads In JGShining‘s Kingdom

Constructing Roads In JGShining’s Kingdom

http://acm.hdu.edu.cn/showproblem.php?pid=1025

分析

  • 最长上升子序列
  • O(nlogn)

代码

// hdu 1025 Constructing Roads In JGShining's Kingdom
#include 
using namespace std;
#define MXN 500010
#define fmt "Case %d:\nMy king, at most %d road%s can be built.\n\n"
int a[MXN], dp[MXN], n, len;
int main() {
     
	int p, r, t = 0;
	while (scanf("%d", &n) == 1) {
     
		for(int i = 1; i <= n; ++i){
     
            scanf("%d %d", &p, &r);
            a[p] = r;
        }
        len = 1, dp[1] = a[1];
        for(int i = 2; i <= n; ++i){
     
            if(dp[len] < a[i]) dp[++len] = a[i];
            else *lower_bound(dp+1, dp+len+1, a[i]) = a[i];
        }
		printf(fmt, ++t, len, len==1?"":"s");
	}
	return 0;
}

你可能感兴趣的:(算法入门,#,动态规划初步)