1. 程式人生 > >HDU-1905-Bridging signals(二分求最長上升子序列)

HDU-1905-Bridging signals(二分求最長上升子序列)

Bridging signals

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1167    Accepted Submission(s): 765

Problem 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 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.

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

考察點:二分法求最長上升子序列

題目大意:如圖所示,左右節點間搭有橋,先在要求去掉部分橋,使剩下的橋儘可能多且不交叉.輸出餘下橋的數目

題目解析: 乍一看不好下手,仔細分析後可以發現:如果要求不交叉,左邊的起點是單調遞增,保證右邊的也是單調遞增,那麼連線一定不會交叉(此處自己仔細想一想即可理解),剩下的就是找如何實現找最長單調遞增子序列.

假設序列一是自然數,序列二如下(隨便敲得),那麼求其最長上升子序列長度可以用如下方法:依次讀取原序列,如果此數大於s[i]且小於s[i-1]那麼覆蓋s[i],如果大於最後一位那麼存進s[i+1].

原序列: 9 2 1 9 3 8 2 7 3 6 2 7 4 6 3 8 6 1 9 3 7 1 2 9 4 7 3 2 4 2 3 0 1 4 3

9 S: 9 第一位

2 S: 2  2<9覆蓋9

1 S: 1  1<2覆蓋2

9 S: 1 9 9>1填進下一位

3 S: 1 3 3<9覆蓋9

8 S: 1 3 8 8>3填進下一位

2 S: 1 2 8 2>1且2<3,覆蓋3

...............

以後的自己推,新序列的長度就是最長上升子序列的長度(得到的不是最長的序列!!).原理琢磨一下,不好說明

實現找到插在那一位使用二分,因為s陣列有序且資料有40000,可能會超時

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
	int T,p,i,x,top;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&p);
		i=0;
		int s[50000]={0};top=0;//初始化s陣列,top記錄陣列長度
		while(p--){
			scanf("%d",&x);
			if(x>s[top])
				s[++top]=x;//s陣列是單調遞增的,如果大於最後一位即大於所有,放最後一位後
			else{
				i=upper_bound(s,s+top,x)-s;//否則用STL的二分函式找到要插入的<span style="color:#ff0000;">位置(地址)</span>,所以要減去s首地址得到陣列下標
				s[i]=x;
			}
		}
		printf("%d\n",top);
	}
}

 注意,此處理方法很巧妙,但是隻能得到最長的序列有多長,而不能得到最長的序列,如需輸出序列,需要另尋方法