HDU 1025 DP+二分求解最长上升序列

来源:互联网 发布:大学生单片机自学视频 编辑:程序博客网 时间:2024/05/18 00:01

Constructing Roads In JGShining’s Kingdom
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18912 Accepted Submission(s): 5357

Problem Description
JGShining’s kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.

Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they’re unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don’t wanna build a road with other poor ones, and rich ones also can’t abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.

The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities … And so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.

In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^

Input
Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.

Output
For each test case, output the result in the form of sample.
You should tell JGShining what’s the maximal number of road(s) can be built.

Sample Input
2
1 2
2 1
3
1 2
2 3
3 1

Sample Output
Case 1:
My king, at most 1 road can be built.

Case 2:
My king, at most 2 roads can be built

1.以往求解最长上升序列时,先是外层循环遍历a[i],对于每个a[i],遍历a[0]到a[i-1]找出a[j] 小于a[i]且dp[j]最大的,然后dp[i]为那个最大值加一。很明显,这种算法嘴坏时间复杂度为O(n^2)。不算好。

2.考虑另一种算法,dp[]里放的不是最大子序列的个数而是能取到a[i]的子序列最后一个数,将dp[]初始化为INF。我们知道,在上升序列中,当前放的元素越小越好。转移方程即为dp[i] = max(dp[i],a[j]);最后用序列中第一个INF的位置减去填充的一个元素的位置就是上升序列的个数。

3.第2种算法的实现复杂度现在看来还是O(n^2),但是第二种算法有优化的做法,第二种算法可以发现是有序填充的,因此dp[]是一个非递减序列。在有序序列中我们可以二分查找大于等于a[j]的第一元素,然后用a[j]替换之。这样时间复杂度就降低到了O(nlogn).

4.对于二分查找,这里用了lower_bound这个函数,函数lower_bound()在first和last中的 前闭后开 区间进行二分查找,返回 大于或等于 val的第一个元素位置。如果所有元素都小于val,则返回last的位置。注意last位置已经越界了。还有函数upper_bound(),upper_bound的作用是返回 大于 val的第一个位置,因此可以用upper_bound(a,a+n,k)-lower_bound(a,a+n,k)来求长度为n的序列中有多少个k。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int a[500010];int dp[500010];int main(){    int N;    int CNT = 1;    while(scanf("%d",&N)!=EOF){        int c,b;        int i,j;        for(i = 0;i<N;i++){            scanf("%d%d",&c,&b);            a[c] = b;        }        memset(dp,0x3f,sizeof(dp));        for(i = 1;i<=N;i++){            *lower_bound(dp+1,dp+N+1,a[i]) = a[i];        }        int sum = lower_bound(dp+1,dp+N+1,0x3f3f3f3f)-dp-1;        printf("Case %d:\n",CNT++);        if(sum == 1)            printf("My king, at most %d road can be built.",sum);        else            printf("My king, at most %d roads can be built.",sum);        printf("\n\n");    }    return 0;}
0 0
原创粉丝点击