描述


http://poj.org/problem?id=1631

铁路左右相连,要求去掉一些边,使得剩下的边不交叉,求剩余边数的最大值.

Bridging signals
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12652   Accepted: 6898

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

Source

分析


当去掉一些边,剩下的边不交叉时,对于左边第 i 的连有边的点,设它连的右边的点为 k , 那么左边第 ( i+1 ) 个连有边的点连的右边的点为了保证边不交叉,一定在 k 的下面.这样的话剩下的边连接的右边的点就是单增的.问题就转化为求边的最长上升子序列.

 #include<cstdio>
#include<algorithm>
#define read(a) a=getnum()
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
using namespace std; const int maxn=,INF=0x7fffffff;
int q,n;
int a[maxn],dp[maxn]; inline int getnum(){ int r=,k=;char c;for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;for(;c>=''&&c<='';c=getchar()) r=r*+c-''; return r*k; } int bsearch(int l,int r,int v)
{
while(l<r)
{
int m=l+(r-l)/;
if(dp[m]>=v) r=m;
else l=m+;
}
return l;
} void solve()
{
for1(i,,n+) dp[i]=INF;
for1(i,,n)
{
int idx=bsearch(,n,a[i]);
dp[idx]=a[i];
}
int ans=bsearch(,n+,INF)-;
printf("%d\n",ans);
} void init()
{
read(q);
while(q--)
{
read(n);
for1(i,,n) read(a[i]);
solve();
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("bridge.in","r",stdin);
freopen("bridge.out","w",stdout);
#endif
init();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("bridge.out");
#endif
return ;
}

POJ_1631_Bridging_Signals_(动态规划,LIS)的更多相关文章

  1. 非 动态规划---LIS

    题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度.(见动态规划---LIS) /* 题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度 ...

  2. 2021.12.06 P2501 [HAOI2006]数字序列(动态规划+LIS)

    2021.12.06 P2501 [HAOI2006]数字序列(动态规划+LIS) https://www.luogu.com.cn/problem/P2501 题意: 现在我们有一个长度为 n 的整 ...

  3. BZOJ_1609_[Usaco2008_Feb]_Eating_Together_麻烦的聚餐_(动态规划,LIS)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1609 给出一串由1,2,3组成的数,求最少需要改动多少个数,使其成为不降或不升序列. 分析 法 ...

  4. 动态规划-LIS最长上升子序列

    优化链接 [https://blog.csdn.net/George__Yu/article/details/75896330] #include<stdio.h> #include< ...

  5. HDU - 1160 FatMouse's Speed 动态规划LIS,路径还原与nlogn优化

    HDU - 1160 给一些老鼠的体重和速度 要求对老鼠进行重排列,并找出一个最长的子序列,体重严格递增,速度严格递减 并输出一种方案 原题等于定义一个偏序关系 $(a,b)<(c.d)$ 当且 ...

  6. HDU-1051/POJ-1065 Wooden sticks 木棍子(动态规划 LIS 线型动归)

    嘤嘤嘤,实习半年多的小蒟蒻的第一篇博客(题解) 英文的: There is a pile of n wooden sticks. The length and weight of each stick ...

  7. POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)

    描述 http://poj.org/problem?id=1065 木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次). Wo ...

  8. 动态规划-LIS

    https://vjudge.net/contest/297216?tdsourcetag=s_pctim_aiomsg#problem/E #include<bits/stdc++.h> ...

  9. P1091 合唱队形题解(洛谷,动态规划LIS,单调队列)

    先上题目 P1091 合唱队形(点击打开题目) 题目解读: 1.由T1​<...<Ti​和Ti​>Ti+1​>…>TK​可以看出这题涉及最长上升子序列和最长下降子序列 2 ...

随机推荐

  1. 使用PDO连接多种数据库

    在PHP 5之前,想要连接MySQL数据库就需要使用mysql或mysqli等一系列函数来操作数据库.例如,我们使用mysql系列数据库函数进行查询操作,对应的示例代码如下: <?php //创 ...

  2. 三、使用Maven构建简单的java项目

    前边,我刚搭建了Maven环境,还有给大家推荐了学习资源,这个小节,我们来就来,,简单的玩玩maven. 1.所需工具: 1.Eclipse     2.apache-maven-3.3.9   3. ...

  3. 利用Highcharts制作web图表学习(二)

        最近中海油的项目需要用到图表展示数据,最近还是一直边学习边开发,今天做了一个展示,炼化厂加热炉效率展示的柱状图,把代码贴出来,大家指点一下互相学习,我是通过数组给Highcharts绑定的值, ...

  4. iOS 指纹解锁

    目前常用的App支持指纹解锁的还不是很多,如果在你的项目中用一下是不是显得高大上呢? 废话不说多,干货- 1.在工程中添加LocalAuthentication.framework 2.在需要验证的c ...

  5. ios专题 - 异步下载加下载进度显示

    [罗国强原创] 今天被刺激了,愤概地要写下这边博文. 说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection  NSURLRequest NSMutableURLReq ...

  6. 关于cnpm的一点小bug

    在实际工作中,一个项目完成后,在上线前,常常需要把代码进行压缩,一般是用gulp或者 webpack 进行压缩.(小妹是用gulp) gulp是运行在node 环境下的. 所以首先,下载并安装了nod ...

  7. Mysql笔记【3】-SQL约束

    SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). 我们将主要探讨以下几种约 ...

  8. jeesite 经常出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL解决思路

    本来jeesite是用的maven,但是我开发的项目用这个框架没用maven, 经常报这个错,我能用的办法就是将springmvc—web.jar包删掉,然后重新添加

  9. 09.13日记(2014年9月13日00:18:26)英语,bootstrap,阮一峰,

    我们这里只推荐一本语法书:台湾的旋元佑老师写的<文法俱乐部>(简体版名为<语法俱乐部>).这本书因为出版社倒闭而绝版,淘宝可以买到影印的版本. (1)学英语:奶爸的英语教室 资 ...

  10. 九度OJ 1373 整数中1出现的次数(从1到n整数中1出现的次数)

    题目地址:http://ac.jobdu.com/problem.php?pid=1373 题目描述: 亲们!!我们的外国友人YZ这几天总是睡不好,初中奥数里有一个题目一直困扰着他,特此他向JOBDU ...