Bridging signals(二分 二分+stl dp)
Bridging signalsTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem 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 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. 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
|
题解:二分水过,dp超时,就是求递增的长度,跟最长单调子序列稍有不同,这个可以用二分,随时更新前面小的元素;
二分:
#include<stdio.h>
int a[];
int main(){
int T,M,top,l,r,mid,m;
scanf("%d",&T);
while(T--){top=;
scanf("%d",&M);
scanf("%d",&m);
a[top]=m;l=;r=top;
for(int i=;i<M;i++){l=;r=top;mid=;
scanf("%d",&m);
if(m>a[top])a[++top]=m;
else{
while(l<=r){
mid=(l+r)/;
if(a[mid]>m)r=mid-;
else l=mid+;
}
a[l]=m;}
}//for(int i=0;i<=top;++i)printf("%d ",a[i]);
printf("%d\n",top+);
}
return ;
}
二分+stl:
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[];
int main(){
int T,N,m,top,l,r,mid;
scanf("%d",&T);
while(T--){top=;
scanf("%d",&N);
scanf("%d",&m);
a[top]=m;
for(int i=;i<N;i++){l=;r=top;
scanf("%d",&m);
if(m>a[top])a[++top]=m;
else *lower_bound(a,a+r,m)=m;
}
printf("%d\n",top+);
}
return ;
}
dp超时:
#include<stdio.h>
#include<string.h>
#define MAX(x,y) x>y?x:y
int dp[];
int m[];
int main(){
int T,N;
scanf("%d",&T);
while(T--){memset(dp,,sizeof(dp));
scanf("%d",&N);
for(int i=;i<N;++i){scanf("%d",&m[i]);dp[i]=;
for(int j=;j<i;j++){
if(m[i]>=m[j])dp[i]=MAX(dp[j]+,dp[i]);
}
}
printf("%d\n",dp[N-]);
}
return ;
}
Bridging signals(二分 二分+stl dp)的更多相关文章
- hdoj 1950 Bridging signals【二分求最大上升子序列长度】【LIS】
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1631 Bridging signals (二分||DP||最长递增子序列)
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9234 Accepted: 5037 ...
- hdu 1950 Bridging signals 求最长子序列 ( 二分模板 )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 3586 二分答案+树形DP判定
HDU 3586 『Link』HDU 3586 『Type』二分答案+树形DP判定 ✡Problem: 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列)
Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列) Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...
- BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP
BZOJ_2097_[Usaco2010 Dec]Exercise 奶牛健美操_二分答案+树形DP Description Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的 ...
- [USACO09DEC]音符Music Notes (二分、STL)
https://www.luogu.org/problem/P2969 题目描述 FJ is going to teach his cows how to play a song. The song ...
- POJ 1631 Bridging signals(LIS O(nlogn)算法)
Bridging signals Description 'Oh no, they've done it again', cries the chief designer at the Waferla ...
- POJ 1631 Bridging signals
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9441 Accepted: 5166 ...
随机推荐
- Unity 功夫猫
最近在家里闲着蛋疼,突然看到一个HTML游戏感觉挺可爱的,就把素材拿过来自己写了一遍. 游戏有很多细节还是没有模仿出来. 里面有一个2DUGUI帧动画播放插件,写了我3个通宵. 还是对Unity的扩展 ...
- Android开源库集锦
一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...
- 【剑指offer】替换字符串中的空格
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25002199 剑指offer上的第四道题目,在九度OJ上測试通过,但还是有些问题.由于是用 ...
- SEXTANTE中调用任意C++控制台程序的简单例子
在sextante中单纯利用python或者调用sextante已有算法进行自定义开发,很多情况下速度不咋给力,同样的操作调用QGIS的C++插件比用sextante里的算法要快,有时候快的 还不止一 ...
- OLEDB简介
OLE DB(OLEDB)是微软的战略性的通向不同的数据源的低级应用程序接口.OLE DB不仅包括微软资助的标准数据接口开放数据库连通性(ODBC)的结构化查询语言(SQL)能力,还具有面向其他非SQ ...
- [HeadFirst-JSPServlet学习笔记][第一章:前言与概述]
第一章 前言与概述 web服务器做什么? 答:接收客户请求,然后向客户返回结果 web客户做什么? 答:此处客户指浏览器,web客户允许用户请求服务器上的某个资源,并向用户展现请求的结果. html ...
- Microsoft Visual C++ 不支持long long
Microsoft Visual C++ 不支持long long 在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说, ...
- ora-24247:网络访问被访问控制列表(ACL)拒绝
用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...
- Html5+css3实现3D转动效果
由于最近一直在忙着筹划去上海工作的事情,所以博客更新的速度也就慢了下来.前几天面试了几家公司,也拿到几份offer,总结了一些面试中遇到的问题,最近整理一下会和大家一起分享.今天呢,就和大家分享一下前 ...
- SICP阅读笔记(一)
2015-08-25 008 Foreword QUOTE: I think that it's extraordinarily important that we in compute ...