poj1631 Bridging signals

来源:互联网 发布:天国王朝知乎 编辑:程序博客网 时间:2024/06/01 07:40

Bridging signals
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14087 Accepted: 7619

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

4642631510234567891018876543219589231746

Sample Output

3914

题意就是给你n个棍子,然后每个左端的棍子都会给你相连接的右端的棍子的编号。问你去掉一些棍子,在剩下的棍子不相交的前提下使得剩余的数量最大。

我们想一下相交的情况,就是一个X,这种情况出现在两个棍子的左右端点的上下位置是交叉的。因为题目中给出的是按照左端点从1到n的,所以,我们应当找出一个上升的序列,因为要求数量最大,所以是求一个最长上升子序列,这里n2的做法显然不行,要用,nlogn的方法。这里是利用了二分的方法。

#include <cstdio>#include <cstring>#include <map>#include <algorithm>using namespace std;const int MAXN=80000+7;int num[MAXN],ans[MAXN];int n,m,k;int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=0;i<n;++i)        {            scanf("%d",&num[i]);        }        int top=0;        ans[top++]=num[0];        for(int i=1;i<n;++i)        {            if(num[i]>=ans[top-1])ans[top++]=num[i];            else ans[lower_bound(ans,ans+top,num[i])-ans]=num[i];        }        printf("%d\n",top);    }    return 0;}







1 0
原创粉丝点击