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

来源:互联网 发布:自然之宝 知乎 编辑:程序博客网 时间:2024/05/10 01:17

题目来源:HPU 1950--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);}}

 注意,此处理方法很巧妙,但是只能得到最长的序列有多长,而不能得到最长的序列,如需输出序列,需要另寻方法

 

0 0
原创粉丝点击