hdu1950Bridging signals(求最长上升自序列nlogn算法)
Bridging signals
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2582 Accepted Submission(s): 1665
Problem Description
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 rossing each other, is imminent. Bearing in mind that there may be housands 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?

Figure 1. To the left: The two blocks' ports and their signal mapping (4,2,6,3,1,5). To the right: At most three signals may be routed on the silicon surface without crossing each other. The dashed signals must be bridged.
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 pecifies 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
Output
Sample Input
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
题目大意
求最长上升子序列,数据范围略大。
分析
n方的算法可能会超时,所以用nlogn的算法。
f[i]表示的长度为i的子序列的结尾最小值是多少,每加入一个值时更新,可以用二分查找优化。
我的另一种想法:
可以维护一个set<pair<int,int> > ,pair中的第一个数是值,第二个是坐标,二分查找小于当前值,且坐标也小于当前坐标的最大的数,更新。
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int a[],f[]; int main()
{
int t,n,len;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=; i<=n; ++i) scanf("%d",&a[i]);
len = ;
f[] = a[];
for (int i=; i<=n; ++i)
{
if (a[i]>f[len]) f[++len] = a[i];
else
{
int pos = lower_bound(f+,f+len+,a[i]) - f;
f[pos] = a[i];
}
}
printf("%d\n",len);
}
return ;
}
推荐一篇文章:http://blog.csdn.net/shuangde800/article/details/7474903
hdu1950Bridging signals(求最长上升自序列nlogn算法)的更多相关文章
- 最长不下降序列nlogn算法
显然n方算法在比赛中是没有什么用的(不会这么容易就过的),所以nlogn的算法尤为重要. 分析: 开2个数组,一个a记原数,f[k]表示长度为f的不下降子序列末尾元素的最小值,tot表示当前已知的最长 ...
- 问题 B: 【例9.3】求最长不下降序列(基础dp)
问题 B: [例9.3]求最长不下降序列 时间限制: 1 Sec 内存限制: 128 MB提交: 318 解决: 118[提交][状态][讨论版][命题人:quanxing] 题目描述 设有由n( ...
- 算法复习——求最长不下降序列长度(dp算法)
题目: 题目背景 161114-练习-DAY1-AHSDFZ T2 题目描述 有 N 辆列车,标记为 1,2,3,…,N.它们按照一定的次序进站,站台共有 K 个轨道,轨道遵从先进先出的原则.列车进入 ...
- JDOJ 1929: 求最长不下降序列长度
JDOJ 1929: 求最长不下降序列长度 JDOJ传送门 Description 设有一个正整数的序列:b1,b2,-,bn,对于下标i1<i2<-<im,若有bi1≤bi2≤-≤ ...
- 最长不下降子序列nlogn算法详解
今天花了很长时间终于弄懂了这个算法……毕竟找一个好的讲解真的太难了,所以励志我要自己写一个好的讲解QAQ 这篇文章是在懂了这个问题n^2解决方案的基础上学习. 解决的问题:给定一个序列,求最长不下降子 ...
- 最长上升子序列O(nlogn)算法详解
最长上升子序列 时间限制: 10 Sec 内存限制:128 MB 题目描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.我们想知道此时最长上升子 ...
- hdu 3308 线段树,单点更新 求最长连续上升序列长度
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 求最长回文子串——Manacher算法
回文串包括奇数长的和偶数长的,一般求的时候都要分情况讨论,这个算法做了个简单的处理把奇偶情况统一了.算法的基本思路是这样的,把原串每个字符中间用一个串中没出现过的字符分隔开来(统一奇偶),用一个数组p ...
- (转载)最长递增子序列 O(NlogN)算法
原博文:传送门 最长递增子序列(Longest Increasing Subsequence) 下面我们简记为 LIS. 定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则 ...
随机推荐
- subline 安装 package control 连接服务器失败,解决办法
解决办法: 打开C:\Windows\system32\drivers\etc\hosts文件 增加 50.116.34.243 sublime.wbond.net50.116.34.243 pack ...
- 【转发活动】Hey, 是你吗? | 寻"粉"启示
你知道吗 从 A computer on every desk and in every home 让每张办公桌上和每个家庭都有一台计算机 ▼ 到 Where do you want to go to ...
- LeetCode Reverse Words in a String 将串中的字翻转
class Solution { public: void reverseWords(string &s) { string end="",tem="" ...
- Centos7_Minimal-1611 版安装python3.5.3
前提 最近在学习python3,看到好多教程都是要求在Windows或者Ubuntu 平台上使用,安装比较方便.由于不在想Winddows上安装也没有Ubutnu系统 ,所以在自己的CentOS7上面 ...
- linux 命令——33 df(转)
linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况.可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息. 1.命令格式: df [选项] [文件] 2.命 ...
- eclipse Indigo搭建SSH框架详解
SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作. 下载好eclipse,Struts2,Spring,Hi ...
- World Wind Java开发之十一——加载热点信息(仿Google Earth)(转)
在GE的图层中有一个照片图层,在浏览时可以看到各地的一些图片,我们称之为热点信息,如下图所示: 再来看下本文的实现效果: 效果是不是很像呢,其实实现这个很简单,参照examples中的Balloons ...
- hdu-1556 Color the ball---树状数组+区间修改单点查询
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...
- python_57_高阶函数
变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数. def add(a,b,f): return f(a)+f(b) res=add(-3,- ...
- TypeScript task
Ctrl+Shift+B 生成 js 文件.