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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值