LIS,只求长度,不求串的具体内容
// hdu 1025 // dp LIS binary_search // Feb.16 2015 #include <cstdio> #define MAXN 500000 int dp[MAXN + 10], n, road[MAXN + 10], case_num = 0, len; // dp[i] 代表长度为i的LIS的末尾数字 // int locate_time = 0; int binary_search(int num) { // 找到一个x的值,使得num刚好不小于dp[x] // 使得 num 可以添加到这个dp[x]对应的LIS后面 int l = 1, r = len; while(l <= r){ int mid = (l+r)>>1; if(num == dp[mid]){ // printf("The %d time...%d equal to dp[%d]\n", ++locate_time, num, mid); return mid; } if(dp[mid] < num) l = mid + 1; if(dp[mid] > num) r = mid - 1; } // printf("The %d time...locate the pos %d\n", ++locate_time, l); return l; } int main(int argc, char const *argv[]) { freopen("in", "r", stdin); while(~scanf("%d", &n)){ for(int i = 1, poor, rich; i <= n; ++i){ scanf("%d%d", &poor, &rich); road[poor] = rich; } dp[1] = road[1]; len = 1; // 初始状态,LIS长度为1,且LIS末尾数字,为road[1] for(int i = 1; i <= n; ++i){ int temp = binary_search(road[i]); // 当前road[i]的值最多能够放到长度为temp的LIS后面 // 然后更新这个长度为temp的LIS的值 dp[temp] = road[i]; // 如果长度为temp的LIS是当前最长LIS,则更新len值 if(temp > len) ++len; } printf("Case %d:\nMy king, at most %d road", ++case_num, len); if(len > 1) printf("s"); printf(" can be built.\n\n"); } }
时间: 2024-10-03 01:39:13