Bridging signals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12251   Accepted: 6687

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
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = + ;
int a[MAX],d[MAX]; //a是原始数据,d是递增子序列
int Find(int c[],int len, int x)
{
int l = ,r = len;
int mid;
while(l <= r)
{
mid = (l + r) / ;
if(c[mid] == x)
return mid;
else if(c[mid] > x)
r = mid - ;
else if(c[mid] < x)
l = mid + ;
}
return l;
}
int main()
{
int t,n,len;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
len = ;
d[] = a[];
for(int i = ; i <= n; i++)
{
int j = Find(d,len,a[i]);
d[j] = a[i];
if(j > len)
len = j;
}
printf("%d\n",len);
}
return ;
}

二分

poj1631Bridging signals(最长单调递增子序列 nlgn)的更多相关文章

  1. 动态规划-最长单调递增子序列(dp)

    最长单调递增子序列 解题思想:动态规划 1.解法1(n2) 状态:d[i] = 长度为i+1的递增子序列的长度 状态转移方程:dp[i] = max(dp[j]+1, dp[i]); 分析:最开始把d ...

  2. [C++] 动态规划之矩阵连乘、最长公共子序列、最大子段和、最长单调递增子序列、0-1背包

    一.动态规划的基本思想 动态规划算法通常用于求解具有某种最优性质的问题.在这类问题中,可能会有许多可行解.每一个解都对应于一个值,我们希望找到具有最优值的解. 将待求解问题分解成若干个子问题,先求解子 ...

  3. HD1160FatMouse's Speed(最长单调递增子序列)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  5. NYOJ17 最长单调递增子序列 线性dp

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=17 分析: i=1 dp[i]=1 i!=1 dp[i]=max(dp[j]+1) ...

  6. nyoj 单调递增子序列(二)

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...

  7. nyist oj 214 单调递增子序列(二) (动态规划经典)

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 ,a2...,an}(0<n<=100000).找出单调递增最长子序列,并求出其长度 ...

  8. ny214 单调递增子序列(二) 动态规划

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...

  9. nyoj 214 单调递增子序列(二)

    单调递增子序列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 ,a2...,an}(0<n<=100000),找出单调递增最长子序列,并求出其长度. ...

随机推荐

  1. 实战 SQL Server 2008 数据库误删除数据的恢复

    SQL Server中误删除数据的恢复本来不是件难事,从事务日志恢复即可.但是,这个恢复需要有两个前提条件: 1. 至少有一个误删除之前的数据库完全备份. 2. 数据库的恢复模式(Recovery m ...

  2. webapi同一个Controller多个函数

    现在用webapi发现一个问题,比如用get方法调用的的时候函数名好像没用,不管你写什么名称,它只认调用相同参数的一个函数,而且以get开头的函数虽然名称不同但是只要参数相同就会报错,无法调用怎么回事 ...

  3. GEOS库的学习之二:简单几何图形的创建

    几何图形(Geometry)是geos里面基本的操作对象,因此Geometry类就是最重要的一个类 几何图形中主要有三个要素:点,线,面.横纵坐标构成点,多个点构成线,环线构成面,点线面混合构成几何集 ...

  4. 自定义GrildView实现单选功能

    首先看实现功能截图,这是一个自定义Dialog,并且里面内容由GrildView 绑定数据源,实现类似单选功能. 首先自定义Dialog,绑定数据源 自定义Dialog弹出框大小方法 最主要实现的就是 ...

  5. linux内核分析 第八周

    第八周 理解进程调度时机跟踪分析进程调度与进程切换的过程 一.进程调度与切换 1.进程的调度时机与进程切换 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程 ...

  6. 详解 ManualResetEvent

    今天详细说一下ManualResetEvent 它可以通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源 当一个线程开始一个活动(此活动必须完成后,其他线程 ...

  7. PRML读书会第三章 Linear Models for Regression(线性基函数模型、正则化方法、贝叶斯线性回归等)

    主讲人 planktonli planktonli(1027753147) 18:58:12  大家好,我负责给大家讲讲 PRML的第3讲 linear regression的内容,请大家多多指教,群 ...

  8. mysqldumpslow使用说明

    mysqldumpslow使用说明 mysqldumpslow --help Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] Parse and summar ...

  9. CSS3小分队——进击的border-radius

    上一篇:<CSS float属性小解——”浮“生若水> 写在前面: ~~强势插入~~如果有想进一步了解float属性的小伙伴,可以猛戳上面的链接,<CSS float属性小解——”浮 ...

  10. IT男的”幸福”生活"续7

    哇,兄弟们的热情真牛X的高呀!!! 在这里谢谢大家了.同时祝大家元宵节快乐,幸福美好.呵呵. 续7准备开始了… ….. 是什么样的事困扰她了… 躺在床上我,一直回想着MM,今天的情绪状态. 分析一下: ...