首页 > 技术文章 > 最长上升子序列变形题-二分查找DP

littlehoom 2014-02-16 13:00 原文

Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13752    Accepted Submission(s): 3920


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.
 

 大意:给两列数据A,B;A列与B列的第i行可以连一条线,求不相交情况下线最多多少条。

我的思路是先从小到大排按B列的数排序,然后从A列里找最大上升子序列。

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 500002
#define INF (1<<20)
using namespace std;
struct node{ int p, r; };
int dp[MAX];
node v[MAX];
bool cmp(node a, node b){
    return a.r < b.r;
}
int search(int x, int h, int l){
    if (h == l)return h;
    int i = (h + l) / 2;
    if (x < dp[i]){
        search(x, h, i);
    }
    else{
        search(x, i + 1, l);
    }
}
int main()
{
    int n, c = 0, j;
    while (scanf("%d", &n) != -1){
        c++;
        for (int i = 0; i < n; i++){
            cin >> v[i].p >> v[i].r;
        }
        sort(v, v + n, cmp);
        fill(dp, dp + n, INF);
        for (int i = 0; i < n; i++){
            j = search(v[i].p, 0, n-1);
            if (j == 0 || dp[j] > v[i].p){
                dp[j] = min(dp[j], v[i].p);
            }
        }
        for (j = 0; j < n; j++){
            if (dp[j] == INF)break;
        }
        printf("Case %d:\n", c);
        if (j == 1){
            printf("My king, at most %d road can be built.\n\n", j);
        }
        else{
            printf("My king, at most %d roads can be built.\n\n", j);
        }
    }
    
    return 0;
}

这个排最长子序列的算法思想是:dp[i]存放长度为i+1的子序列末尾数的最小值。可以看出这个值是逐渐增加的,所以可以利用二分查找更新这个数组。如果你没学过动态

规划,那么你就可以把它看成是取扑克牌,只要比手里最大还大就留着,小的话就替掉一张。时间复杂度是N*log(n)。这道题如果用N*N,肯定超时!

推荐阅读