[dp]最长递增子序列poj 1631Bridging signals

来源:互联网 发布:淘宝冻结资金 编辑:程序博客网 时间:2024/05/16 08:41

poj 1631Bridging signals Dp(最长递增子序列)

Bridging signals
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8689 Accepted: 4712
Description
‘Oh no, they’ve done it again’, cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without crossing each other, is imminent. Bearing in mind that there may be thousands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task?

A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number specifies which port on the right side should be connected to the i:th port on the left side.Two signals cross if and only if the straight lines connecting the two ports of each pair do.
Input
On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer p < 40000, the number of ports on the two functional blocks. Then follow p lines, describing the signal mapping:On the i:th line is the port number of the block on the right side which should be connected to the i:th port of the block on the left side.
Output
For each test scenario, output one line containing the maximum number of signals which may be routed on the silicon surface without crossing each other.
Sample Input
4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
10
1
8
8
7
6
5
4
3
2
1
9
5
8
9
2
3
1
7
4
6
Sample Output
3
9
1
4
Source
Northwestern Europe 2003
题意:给定1个序列,不会重复,x出现在y位置,即代表x与y可以连线。最后要从这个序列中尽量多地选择连线并保证连线不交叉。
解题思路:其实这题就是两个序列连线,求连线不交的最多连线数。本题一个序列已经固定,那只要保证另一列被选择的数是递增的就不会相交了,不理解的话画几个模拟下就懂了。那问题就转化为最长递增子序列了。一般的解法时间复杂度是n^2,但这题数据较大,必须要有更高效的解法。由于每次更新最长序列长度都是根据已有的长度,试想用一个数组来模拟这个过程,数组下标代表长度,数组内容代表当前长度最小的数值。这样更新答案只要从下标最大的那个地方开始比较就ok。由于长度是递增的,可以用二分查找来找每次更新的那个下标。

基本思想:
dp
设m[i]表示映射到{1,2,…,i}的最长不下降序列的长度,则有
当 0<=j<=i-1, data[i]>data[j] 时, m[i] = max(m[j],m[j] + 1);
算法复杂度O(n^2)

技巧:设置一个数组a[i]存放所有长度为i的上升子序列中最小的末元素值,比如说只有两个长度为3的上升子序列123和124,那么a[3]中存放的就是3(末元素3<4)。那么当来一个新数data时,如果它的值大于最长长度的末元素的值(即a[ans]),则ans++;且a[ans]=data;
否则,通过二分查找(数组a中的元素为递增),将最接近data且大于data的那个元素更新为data,既最小的大于它的数。例如1,5,3,4,之后来个2,a[1]=1,a[2]=3,a[3]=4;则更新a[2]=2;由于二分查找复杂度为log(n),外围为n,总的复杂度为nlogn

#include <stdio.h>int res[40000];int binSearch(int left, int right, int num)         //找到最小的大于等于它的数{    while(left <= right)    {        int mid=(left + right)/2;         if(res[mid]<num) left=mid+1;        else  right=mid-1;    }    return right;}int main(){    int t, n, num;    scanf("%d", &t);    while(t--)    {        scanf("%d %d", &n, &num);        res[0]=num;        int tot = 1;        for(int i=1;i<n;++i)        {            scanf("%d", &num);            if(num>=res[tot-1])            res[tot++]=num;            else            {                int pos=binSearch(0,tot-1,num);      //找到最小的大于它的数                res[pos+1] = num;            }          }        printf("%d\n", tot);    }    return 0;}

欢迎留言,积极讨论,一起进步!

0 0
原创粉丝点击