描述


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. 学习笔记_过滤器应用(粗粒度权限控制(拦截是否登录、拦截用户名admin权限))

    RBAC ->基于角色的权限控制 l  tb_user l  tb_role l  tb_userrole l  tb_menu(增.删.改.查) l  tb_rolemenu 1 说明 我们给 ...

  2. ZeroMemory

    ZeroMemory: 用0填充一个内存块 void ZeroMemory( [in] PVOID Destination, //内存块开始地址 [in] SIZE_T Length //填充块大小 ...

  3. java swing窗口放置屏幕中央问题思考

    java swing窗口放置屏幕中央问题思考 以前总是尝试各种方法都没有能把组件放到屏幕中央,只能用死办法,设置绝对坐标,但这样就失去了可移植性,而且繁琐.今天仔细思考了一番,终于被我找出问题所在. ...

  4. Ubuntu 使用top/free查看内存占用大的原因

    Ubuntu 使用top/free查看内存占用大的原因     linux/ubuntu下free/top查看内存占用大的原因 使用free/top查看内存占用的时候,吓了一大跳,机器4GB的内存,显 ...

  5. 洛谷 U3178 zty的冒险之行

    U3178 zty的冒险之行 题目提供者mangoyang 题目背景 "妈咪妈咪轰"随着一声巨响,zty传送到了Aluba国,在那里浴血奋战,饱读兵书,风餐露宿,吃喝嫖赌,终于到了 ...

  6. Bzoj 2431 HAOI2009 逆序对数列

    Description 对于一个数列{ai},如果有i**<**j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数. ...

  7. UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0

  8. SQL Server系统视图 [不定期更新]

    1.sys.objects:在数据库中创建的每个用户定义的架构作用域内的对象(如表.视图.约束.默认值.日志.规则存储过程等,但不包括DDL触发器)在该表中均对应一行. 列名 说明 name 对象名. ...

  9. ubuntu基本使用

    sudo nautilus xxx指定目录去打开 这个命令就是以root权限打开一个窗口,来管理文件

  10. skymvc网站测试之mysql数据生成

    skymvc网站测试之mysql数据生成 使用方法: 删除数据 /index.php?m=test_mysql&a=autoDelete 重置自增ID /index.php?m=test_my ...