描述


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. C# 简单的图像边缘提取

    博主做的很简单,大家看一看就好了...... 用到的算法是robert算子,这是一种比较简单的算法: f(x,y)=sqrt((g(x,y)-g(x+1,y+1))^2+(g(x+1,y)-g(x,y ...

  2. switch case实现两个数的算术运算

    方法一: package com.liaojianya.chapter1; import java.util.Scanner; public class SwitchDemo1 { public st ...

  3. java.util.HashMap源码分析

    在java jdk8中对HashMap的源码进行了优化,在jdk7中,HashMap处理“碰撞”的时候,都是采用链表来存储,当碰撞的结点很多时,查询时间是O(n). 在jdk8中,HashMap处理“ ...

  4. iframe框根据内容自适应高度

    1.页面 <iframe name="iframe_userCenter" id="iframe" frameborder=2 width=100% he ...

  5. 自定义注解与MYSQL

    无聊之作,可以提意见,但别嘲笑啊 package bean; import java.sql.Date; import annotationK.annotation.Column; import an ...

  6. 在阿里云服务器ubuntu14.04运行netcore

    从netcore1.0正式发布就很激动,想要赶紧学习. 最近博客园的一篇文章给了完整的指导非常感谢,但是在实际实现到发布到阿里云服务器遇到一些问题,记录下来. 首先上基础文章http://www.cn ...

  7. 深入理解Python中的生成器

    生成器(generator)概念 生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常结束. 生成器语法 生成器表达式: 通列表解 ...

  8. git 401 错误

    错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...

  9. svn-代码回滚

    第一种:# svn revert [-R] something 第二种: 1. svn update,svn log,找到最新版本(latest revision)    2. 找到自己想要回滚的版本 ...

  10. 网络编程TCP/IP实现客户端与客户端聊天

    一.TCP/IP协议 既然是网络编程,涉及几个系统之间的交互,那么首先要考虑的是如何准确的定位到网络上的一台或几台主机,另一个是如何进行可靠高效的数据传输.这里就要使用到TCP/IP协议. TCP/I ...