描述


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. iOS--检测野指针

    定位野指针除了使用Malloc Scribble(内存涂鸦)外,还可以使用僵尸对象.所谓的僵尸对象,就是将被释放的对象标记为僵尸,系统不会回收这些对象的内存,并让这些内存无法被重用,因而也就不会被覆写 ...

  2. iOS lanchImage 和icon的设置

    1 icon的设置 打开项目中的Assets.xcassets   这里边有一个icon 首先需要有icon 的尺寸 尺寸如下: 29*29   2x 29*29   3x 40*40  2x 40* ...

  3. Content by query webpart 自定义样式的使用方法

    今天研究一个非常实用的webpart 如果在office365 上 的webpart中一直没“内容查询”, 这里需要开启2个features:http://community.office365.co ...

  4. java新手笔记17 参数

    package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, ...

  5. OpenCV学习(1)-安装(Windows)

    下载安装 在这里下载.我下载了2.4.9的Windows版本.双击安装即可. 配置环境变量 配置环境变量的目的是为了让系统找到OpenCV的动态链接库.因此需要把动态链接库添加到系统环境变量PATH中 ...

  6. docker中搭建jenkins环境

    想在docker中搭建一个jenkins环境.开始的时候我想在先pull centos,然后在里面自己搭建环境,搭建后的环境可以运行,但是不知道挂载哪的数据,这也是我不熟悉jenkins的原因. 同事 ...

  7. win7下简单FTP服务器搭建

    本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...

  8. 《sed的流艺术之三》-linux命令五分钟系列之二十三

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  9. 网站加载有商务通、商桥,定义js函数触发快商通代码

    有的网站已经加载了商务通.商桥的,前期定义了js函数 触发商务通.商桥代码的,可以重新定义新的函数对之前的函数进行覆盖,其 js代码为: var domain = document.domain; / ...

  10. PHP程序实现利用rand(1,100)函数产生10个1~100之间的随机数

    //echo rand(1,100);$max=0;$min=100;for($i=0;$i<=9;$i++){ $rand[$i]=rand(1,100); if($rand[$i]>$ ...