HDU-1950(LCS)(Bridging signals)
Bridging signals
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1476 Accepted Submission(s): 965
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 rossing each other, is imminent. Bearing in mind that there may be housands 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?
Figure 1. To the left: The two blocks' ports and their signal mapping (4,2,6,3,1,5). To the right: At most three signals may be routed on the silicon surface without crossing each other. The dashed signals must be bridged.
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 pecifies 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.
題意:有兩列數,第一列數根據相應關係,和第二列數進行連線。若一條一條連線,最多畫m條線時,這些線互不相交,在畫m+1條線時就有相關線條交叉了。
樣例分析:
如:
6
4
2
6
3
1
5
6表示有6行數,如樣列第一行爲4,表示第一列的第一行數和第二列的第四行數有一條連線。
樣列第二行的2表示:第一列的第二行數和第二列的第二行數之間有一條連線。樣列第三行的6表示:第一列的第三行的數和第二列的第六行的數之間有一條連線。。。。。。。
第一列和第二列的數,都是從1到p的連續的數。
求連線不交叉的最大線條數目,由於第一列的數是順序遞增的,也就是求第二列被連接的數的最大上升子序列的長度。
第一組樣列的結果爲3, 即: 2----2;4------3;6-------5,其中2、3、5是第二列被連接的數的最大上升子序列。
My solution:
/*2016.4.22*/
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int a[44000],dp[40010];
int n,m,h;
int search(int x)//折半查找
{
int i,j,k,left=1,right=h,path;
while(left<=right)
{
path=(left+right)/2;
if(dp[path]<a[x]&&a[x]<=dp[path+1])
return path+1;
else
{
if(dp[path]>a[x])
right=path;
else
left=path;
}
}
}
int main()
{
int i,j,k,t,temp;
scanf("%d",&t);
while(t--)
{
temp=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
temp=dp[1]=a[1];
h=1;
for(i=2;i<=n;i++)
{
if(a[i]>dp[h])
{
h++;
dp[h]=a[i];
}
else if(a[i]<=dp[1])
{
dp[1]=a[i];
}
else
{
k=search(i);
dp[k]=a[i];
}
}
printf("%d\n",h);
}
return 0;
}