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的上升子序列,则 ...
随机推荐
- 如何避开JavaScript浮点数计算精度问题(如0.1+0.2!==0.3)
不知道大家在使用JS的过程中有没有发现某些浮点数运算的时候,得到的结果存在精度问题:比如0.1 + 0.2 = 0.30000000000000004以及7 * 0.8 = 5.60000000000 ...
- Google常用拓展插件
1.web前端助手(FEhelper)提供一些实用的前端小工具,功能十分贴心 2.bookMarks Manager 一个书签管理工具 3.Clear Cache 清除浏览器的缓存,有很多供选择的条目 ...
- Linux、命令ps 各字段意思
参数: -A :所有的进程均显示出来,与 -e 具有同样的效用: -a : 显示现行终端机下的所有进程,包括其他用户的进程: -u :以用户为主的进程状态 : x :通常与 a 这个参数一起使用,可列 ...
- 使用Java程序消费SAP Leonardo的机器学习API
以sap leonardo作为关键字在微信上搜索,能搜到不少文章.但是我浏览了一下,好像没有发现有从具体编程角度上来介绍的.所以我就贡献一篇. 需求 开发一个Java程序,用户可以指定一张图片,该Ja ...
- #linux 命令使用 cp -未完结版
下载了sublime 解压之后,想把文件夹放到opt目录,这里用命令cp将其复制过来 johnny@johnny-pc:~$ sudo cp -r ~/下载/Sublime_2.0.2 /opt/ [ ...
- 站点安全预警,建议大家多重禁止load_file函数!
比如在你的linux机器上运行 select load_file(0x2F6574632F706173737764); 看看结果是什么?这应该不是我们希望看到的. 所以我们禁用这个函数吧. 这个主要通 ...
- 2017.12.14 Java实现-----图书管理系统
通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...
- python_10_for guess
age_of_oldboy=56 count=0 for count in range(3): guess_age=int(input('guess age:')) if guess_age==age ...
- 利用Theano理解深度学习——Multilayer Perceptron
一.多层感知机MLP 1.MLP概述 对于含有单个隐含层的多层感知机(single-hidden-layer Multi-Layer Perceptron, MLP),可以将其看成是一个特殊的Logi ...
- AngularJs学习笔记-数据绑定、管道
数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...