ZOJ-1986 Bridging Signals

Bridging Signals


Time Limit: 2 Seconds      MemoryLimit: 65536 KB


'Oh no, they'vedone it again', cries the chief designer at the Waferland chip factory. Oncemore the routing designers have screwed up completely, making the signals onthe chip connecting the ports of two functional blocks cross each other allover the place. At this late stage of the process, it is too expensive to redothe routing. Instead, the engineers have to bridge the signals, using the thirddimension, so that no two signals cross. However, bridging is a complicatedoperation, and thus it is desirable to bridge as few signals as possible. Thecall for a computer program that finds the maximum number of signals which maybe 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 ofa functional block, the problem asks quite a lot of the programmer. Are you upto the task?



Figure 1. To theleft: The two blocks' ports and their signal mapping (4, 2, 6, 3, 1, 5). To theright: At most three signals may be routed on the silicon surface withoutcrossing each other. The dashed signals must be bridged.

A typicalsituation is schematically depicted in figure 1. The ports of the twofunctional blocks are numbered from 1 to p, from top to bottom. The signalmapping is described by a permutation of the numbers 1 to p in the form of alist of p unique numbers in the range 1 to p, in which the ith number specifieswhich port on the right side should be connected to the ith port on the leftside. Two signals cross if and only if the straight lines connecting the twoports of each pair do.


Input

On the first lineof the input, there is a single positive integer n, telling the number of testscenarios to follow. Each test scenario begins with a line containing a singlepositive integer p < 40000, the number of ports on the two functional blocks.Then follow p lines, describing the signal mapping:

On the ith line isthe port number of the block on the right side which should be connected to theith port of the block on the left side.


Output

For each testscenario, output one line containing the maximum number of signals which may berouted 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

——————————————————————————————————————————————————————————————————————

題目的意思是求一個最大遞增序列,但是用普通的DP解決時間複雜度爲O(n^2),題目提供的數據規模會超時。需要通過二分查找,將時間複雜度改爲O(nlogn)。

定義一個數組,記錄所有等長的遞增子序列的最後一個元素的中最小的那個,然後只要用二分查找找出滿足d[j-1] < array[i] < d[j]的那個數,將它加到d[j]中。如果array[i] > d[j],那麼d[++j] = array[i],最後結果就是j。



#include <stdio.h>

int main()
{
	int n, p, i, len, mid;
	int parray[40001];
	int d[40001], l, r;
	scanf("%d", &n);
	while(n--) {
		scanf("%d", &p);
		for(i = 1; i <= p; i++) {
			scanf("%d", parray+i);
		}
		d[1] = parray[1];
		len = 1;
		for(i = 2; i <= p; i++) {
			if(parray[i] > d[len]) {
				d[++len] = parray[i];
				continue;
			}
			l = 1;
			r = len;
			while(l <= r) {
				mid = (l + r) / 2;
				if(parray[i] < d[mid]) {
					r = mid-1;
				} else {
					l = mid+1;
				}		
			}
			d[l] = parray[i];
		}
		printf("%d\n", len);
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章