题目:

'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

题意:先输入一个数t,表示有t组数据,每组数据第一行有一个p,下面紧接着p行,1<= i <=p,

每行的数字a[i]表示右边的 i 和左边的a[i]相连接,找出在不相交的情况下有多少相连的;

思路:

动态规划的求最大上升子序列问题;

此题数组长度p<=40000, 所以没有优化的求法肯定超时,这里就用到了lower_bound()函数;

用法:https://blog.csdn.net/yao166164474/article/details/53115372

就是定义dp[i]:=长度为i+1的上升子序列中末尾元素的最小值(不存在的话就是INF),

初始化对dp[]数组全部赋值成INF,也就是+∞,然后对每个 aj 如果 i = 0 或者 dp[i-1] < aj 的话,

就用dp[i] = min(dp[i],aj)进行更新。最终找出使得dp[i]<INF的最大的i+1就是结果。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[40010],dp[40010];
int INF=0x3f3f3f3f;
int main()
{int t;scanf("%d",&t);while(t--){memset(dp,0x3f3f3f3f,sizeof(dp));memset(a,0,sizeof(a));int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);int maxx=0;for(int i=1;i<=n;i++){*lower_bound(dp+1,dp+n+1,a[i])=a[i];}printf("%d\n",lower_bound(dp+1,dp+n+1,INF)-dp-1);}return 0;
}

Bridging signals相关推荐

  1. Bridging signals(二分 二分+stl dp)

    欢迎参加--每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 6 ...

  2. Bridging signals(最长上升子序列)

    Bridging signals 题目 "哦,不,他们又做到了",Waferland 芯片厂的首席设计师喊道.布线设计人员再次完全搞砸了,使连接两个功能块端口的芯片上的信号到处都是 ...

  3. HDU1950 Bridging signals 裸LIS

    Bridging signals Bridging signals 题意 代码 题意 HDU1950 裸LIS,信号不能交叉,编号1-N,实际上就是有序上升,趁5773复习了LIS,直接交了一发,基本 ...

  4. POJ 1634 Bridging signals

    Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. ...

  5. HDU 1950 Bridging signals

    那么一大篇的题目描述还真是吓人. 仔细一读其实就是一个LIS,还无任何变形. 刚刚学会了个二分优化的DP,1A无压力. 1 //#define LOCAL 2 #include <iostrea ...

  6. pku1631 Bridging signals

    http://poj.org/problem?id=1631 DP 最长上升子序列 最长上升子序列,因为n很大(n<40000),我开始写的O(n^2)的算法超时了... 学了一下O(n*log ...

  7. 最长连续子序列nlogn算法

    最长上升子序列(LIS)长度的O(nlogn)算法 标签: 算法search优化存储 2012-04-18 19:38 14031人阅读 评论(5) 收藏 举报  分类: 资料学习(15)  解题报告 ...

  8. 《挑战程序设计竞赛(第2版)》习题册攻略

    本项目来源于GitHub 链接: 项目GitHub链接 1 前言 项目为<挑战程序设计竞赛(第2版)>习题册攻略,已完结.可配合书籍或笔记,系统学习算法. 题量:约200道,代码注释内含详 ...

  9. 【转】别人整理的DP大全

    为什么80%的码农都做不了架构师?>>>    动态规划 动态规划 容易: 1018 , 1050 , 1083 , 1088 , 1125 , 1143 , 1157 , 1163 ...

  10. 最长上升子序列三种模板(n^2模板,二分模板,树状数组模板)

    最长上升子序列(LIS)是动态规划的入门.总结下来,经常用的模板一共有三种,分别为n^2模板,二分模板,树状数组模板. n^2模板代码如下: //n^2算法,本质就是dp,采用二重循环的方式.对于数据 ...

最新文章

  1. 工作休息之余的生活乐趣
  2. 查询出来时间不对_2020年一级、二级建造师执业资格考试成绩可查询!
  3. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
  4. java高并发(八)不可变对象
  5. poe交换机标准与非标准的区别介绍
  6. 【北航oj】(线段树取模运算)
  7. 啥?分布式啥?啥事务?
  8. 读书笔记Black-Scholes-Merton之二
  9. 使用CrossOver的Wine配置修改容器WIndows系统版本
  10. 样式中的url加载探疑
  11. ANSYSworkbench中skewness解释
  12. 密探查询系统服务器码,车辆国几排放查询
  13. 组件化之路 - ViewBinding基类封装
  14. 清华大学计算机秦凌霄,海南25名考生获得北大清华自主招生入选资格
  15. CSAPP-Lab03 Attack Lab 详细解析
  16. JavaScript 之 学习网站推荐 强推【javascript.info】
  17. UG汽车模具设计之汽车模斜顶机构的设计思路,建议收藏
  18. 化繁为简,聊一聊复制状态机系统架构抽象
  19. 解决 ArchLinux 下中文 Chinese 不能输入 couldnt input 的问题
  20. 从拖延到高效,我推荐这7本书

热门文章

  1. Linux复制文件到当前目录
  2. html改excel,怎么把html文件转成excel:html表格如何转换excel
  3. RCC BUCK变压器设计
  4. 软考-中级-网络工程师-知识点个人总结(三)
  5. 搜狐全体员工遭遇工资补助诈骗,冲上微博热搜第一
  6. PDF有口令密码怎么移除?
  7. matlab中ga函数的用法,[转载]MATLAB中自带遗传算法函数GA的用法
  8. 未来教育计算机二级答案,未来教育计算机二级操作题答案.docx
  9. PS证件照更改背景颜色
  10. 西湖大学博导:都说不唯论文,那我们发表论文是为了什么?