Bridging signals(求最长上升自序列nlogn算法)

Bridging signals

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

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

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

题目大意

求最长上升子序列,数据范围略大。

分析

n方的算法可能会超时,只能用nlogn的算法。

f[i]表示唱的为i的子序列的结尾最小值是多少,每加入一个值时更新,可以用二分查找优化。

code

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5
 6 int a[50010],f[50010];
 7
 8 int main()
 9 {
10     int t,n,len;
11     scanf("%d",&t);
12     while (t--)
13     {
14         scanf("%d",&n);
15         for (int i=1; i<=n; ++i) scanf("%d",&a[i]);
16         len = 1;
17         f[1] = a[1];
18         for (int i=2; i<=n; ++i)
19         {
20             if (a[i]>f[len]) f[++len] = a[i];
21             else
22             {
23                 int pos = lower_bound(f+1,f+len+1,a[i]) - f;
24                 f[pos] = a[i];
25             }
26         }
27         printf("%d\n",len);
28     }
29     return 0;
30 }

推荐一篇文章:http://blog.csdn.net/shuangde800/article/details/7474903

时间: 2024-08-10 23:29:14

Bridging signals(求最长上升自序列nlogn算法)的相关文章

算法复习——求最长不下降序列长度(dp算法)

题目: 题目背景 161114-练习-DAY1-AHSDFZ T2 题目描述 有 N 辆列车,标记为 1,2,3,-,N.它们按照一定的次序进站,站台共有 K 个轨道,轨道遵从先进先出的原则.列车进入站台内的轨道后可以等待任意时间后出站,且所有列车不可后退.现在要使出站的顺序变为 N,N-1,N-2,-,1,询问 K 的最小值是多少. 例如上图中进站的顺序为 1,3,2,4,8,6,9,5,7,则出站的顺序变为 9,8,7,6,5,4,3,2,1. 输入格式 输入共 2 行.第 1 行包含 1 

求最长不下降序列个数

求最长不下降序列个数(jdoj-1946) 题目大意:给你一个序列,求所有最长不下降序列的个数. 注释:n(总序列长度)<10000. 想法:维护两个数组,分别表示包含这个数的最长子序列长度和达到这个长度的方案数,最后统计答案,跑两次dp即可. 最后,附上丑陋的代码....... 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int dp[10010]; 5 int a[10010]; 6

【模板】求最长不下降序列 [动态规划 LIs]

求最长不下降序列 看不出来哪里还错了..... d[i]以i为结尾的最长上升子序列的长度     g[i]表示d值为i的最小状态的编号即长度为i的上升子序列的最小末尾值(d[j]=i的j值最小) liurujia's for(int i=1;i<=n;++i) g[i]=inf; for(int i=1;i<=n;++i){ int k=lower_buond(g+1,g+1+n,a[i])-g; d[i]=k; g[k]=a[i]; } 二昏好难啊..... 贴上90昏代码.... #inc

最长递增子序列 O(NlogN)算法

https://www.felix021.com/blog/read.php?entryid=1587&page=3&part=1 感谢作者! 标题:最长递增子序列 O(NlogN)算法 出处:Blog of Felix021 时间:Wed, 13 May 2009 04:15:10 +0000 作者:felix021 地址:https://www.felix021.com/blog/read.php?1587  内容: 今天回顾WOJ1398,发现了这个当时没有理解透彻的算法. 看了好久

zoj1986 Bridging Signals (dp,最长递增序列,LIS)

A - Bridging Signals Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1986 Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designer

HDU 1950 Bridging signals【最长上升序列】

解题思路:题目给出的描述就是一种求最长上升子序列的方法 将该列数an与其按升序排好序后的an'求出最长公共子序列就是最长上升子序列 但是这道题用这种方法是会超时的,用滚动数组优化也超时, 下面是网上找的求LIS的算法 假设要寻找最长上升子序列的序列是a[n],然后寻找到的递增子序列放入到数组b中. (1)当遍历到数组a的第一个元素的时候,就将这个元素放入到b数组中,以后遍历到的元素都和已经放入到b数组中的元素进行比较: (2)如果比b数组中的每个元素都大,则将该元素插入到b数组的最后一个元素,并

最长不下降子序列nlogn算法详解

今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子序列的长度(nlogn的算法没法求出具体的序列是什么) 定义:a[1..n]为原始序列,d[k]表示长度为k的不下降子序列末尾元素的最小值,len表示当前已知的最长子序列的长度. 初始化:d[1]=a[1]; len=1; (0个元素的时候特判一下) 现在我们已知最长的不下降子序列长度为1,末尾元素

最长上升子序列O(nlogn)算法详解

最长上升子序列 时间限制: 10 Sec   内存限制:128 MB 题目描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.我们想知道此时最长上升子序列长度是多少? 输入 第一行一个整数N,表示我们要将1到N插入序列中,接下是N个数字,第k个数字Xk,表示我们将k插入到位置Xk(0<=Xk<=k-1,1<=k<=N) 输出 1行,表示最长上升子序列的长度是多少. 样例输入 3 0 0 2 样例输出 2 提示 100%的数据 n&l

poj 1631 Bridging signals DP(最长上升子序列)

最近一直在做<挑战程序设计竞赛>的练习题,感觉好多经典的题,都值得记录. 题意:给你t组数据,每组数组有n个数字,求每组的最长上升子序列的长度. 思路:由于n最大为40000,所以n*n的复杂度不够了,会超时. 书上状态方程换成了d[i]——以长度为i+1的上升子序列中末尾元素的最小值. 那么我们在遍历第i个元素时候,以这个元素为末尾元素的最长子序列也就是在d[i]中找到一个小于num[i]的最大值,然后在这个序列末尾加上num[i] 显然,我们在查找时便可以利用二分搜索,从而把复杂度从原来的