Bridging signals

在O(n^2)的算法中:创建一个一维数组a[j],dir[],p[j]表示序列的元素,dir[i]表示以第i个元素结尾的序列中的最长下降子序列,初始化为1,对于一个dir[i],遍历前面的每个元素j,如果a[j]>a[i]且dir[j]>=dir[i],那么dir[j]就要加1,在这里,遍历前面的每个元素j,寻找此前最大的子序列时间复杂度为O(n),如果我们在一个有序的序列中查找此前最大的序列长度,我们就可以用二分查找,时间复杂度就会降为O(logn),总的时间复杂度就会为O(nlogn)。为此,我们增加一个一维数组p,p[i]表示当前序列为i的末尾元素的最小值。例如对于序列:4 2 6 3 1 5 :


i

1

2

3

4

5

6

a

4

2

6

3

1

5

dir

1

1

2

2

1

3

p

1

3

5

 

 

 

构建过程如下:
i=1时,dir[i]=1 p[i]=4(当前为1的序列的末尾元素的最小值)

dir

1

1

1

1

1

1

p

4

 

 

 

 

 

i=2时,2不大于4,所以dir[i]=1,将p[1]更新为2

dir

1

1

1

1

1

1

p

2

 

 

 

 

 

i=3时,6大于2,所以dir[i]=1+1,将p[2]更新为6

dir

1

1

2

1

1

1

p

2

6

 

 

 

 

i=4时,3在2 6之间,所以dir[i]=1+1,将p[2]更新为3

dir

1

1

2

2

1

1

p

2

3

 

 

 

 

i=5时,1小于2,所以dir[i]=1,将p[1]更新为1

dir

1

1

2

2

1

1

p

1

3

 

 

 

 

i=6时,5大于3,所以dir[i]=2+1,将p[3]更新为5

dir

1

1

2

2

1

3

p

1

3

5

 

 

 

dir[6]就是最后的结果。从构建的过程可以容易的证明一下两点:p是递增的。p是当前序列为i的末尾元素的最小值。以上“2不大于4”,“3在2 6之间”等等的判断采用二分查找,所以总的时间复杂度为:O(nlogn),核心的c代码如下:
for(i=1;i<=n;i++)
{
      num = a[i];
      left = 1;
      right = plen;
      while(left<=right)
      {
          mid = (left+right)/2;
          if(p[mid]<num)
             left = mid+1;
          else
             right = mid-1;
      }
      dir[i] = left;
      p[left] = num;
      if(plen<left)
          plen = left;
     if(max<dir[i])
              max = dir[i];
}

printf("%d\n",max);




2093: Bridging signals 分享至QQ空间 去爱问答提问或回答

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 32            Accepted:16

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

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


AC代码:

#include<stdio.h>
#include<string.h>
int main()
{
	int i, j, n, t, k, dp[40000], p[40000], a[40000], plen, mid, left, right;
	scanf("%d",&t);
	while(t--)
	{
		memset(dp, 0, sizeof(dp));
		memset(p, 0, sizeof(p));
		scanf("%d", &n);
		for(i = 0; i < n; i++)
			scanf("%d", &a[i]);
		plen = 0;
		int max = 0;
		for(i = 0; i < n; i++)
		{
			left = 0; 
			right = plen;
			while(left <= right)
			{
				mid = (right + left) / 2;
				if(p[mid] < a[i])
					left = mid + 1;
				else 
					right = mid - 1;
			}
			dp[i] = left;
			p[left] = a[i];
			if(left > plen)
				plen = left;
			if(dp[i] > max)
				max = dp[i];
		}
		printf("%d\n",max);
	}
}






同学的AC的代码:

#include <stdio.h>
#include <string.h>
int dp[40001],a[40001];
int main()
{
	int t,n,i,j,len,l,mid,r;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i = 1 ; i <= n ; i++)
			scanf("%d",&a[i]);
		dp[1]=a[1];
		len=1;
		for(i = 2 ; i <= n ; i++)
		{
			if(dp[len] < a[i])
			{
				dp[++len]=a[i];
				continue;
			}
			l=1;r=len;
			while(l <= r)
			{
				mid=(l+r)/2;
				if(dp[mid] < a[i])
					l=mid+1;
				else
					r=mid-1;
			}
			dp[l]=a[i];
		}
		printf("%d\n",len);
	}
} 


猜你喜欢

转载自blog.csdn.net/u013780740/article/details/38438325
今日推荐