HDU 1950 Bridging signals(LIS)
2015-10-06 17:28
381 查看
Bridging signals
[align=left]Problem Description[/align]'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.
Two signals cross if and only if the straight lines connecting the two ports of each pair do.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4
6
4
2
6
3
1
5
10
2
3
4
5
6
7
8
9
1
4000
0
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
【思路分析】
说了那么多废话,其实还是要你求LIS(当然还是要锻炼迅速抽象的能力),由于序列长度长达40000,所以还是用的O(nlogn)的方法。
代码如下:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 40005; int n; int a[maxn]; int d[maxn];//记录长度为i的上升子序列最后一个元素的值 int binSearch(int key,int left,int right) { while(left <= right) { int mid = (left + right) >> 1; if(key > d[mid] && key <= d[mid + 1])//贪心策略 { return mid; } else if(key > d[mid]) { left = mid + 1; } else { right = mid - 1; } } return 0; } int LIS(int n) { d[1] = a[1]; int len = 1; int j = 0; for(int i = 2;i <= n;i++) { if(d[len] < a[i]) { j = ++len;//直接向后插入 } else { j = binSearch(a[i],1,len) + 1;//找到替换位置 } d[j] = a[i]; } return len; } void init() { scanf("%d",&n); for(int i = 1;i <= n;i++) { scanf("%d",&a[i]); } } void solve() { printf("%d\n",LIS(n)); } int main() { int t; scanf("%d",&t); while(t--) { init(); solve(); } return 0; }
相关文章推荐
- 多线程时间服务器
- 黑马程序员----Objective-C学习笔记之NSString类的内存管理问题
- Web APi之认证(Authentication)及授权(Authorization)【一】(十二)
- 数组例题
- CSS选择器的阅读方式
- Implementing a simple smart pointer in C++
- 交换两个变量的值,不借助第三方变量 ---指针篇
- Add Two Numbers
- ubuntu15.04手动安装MySQL5.6.27数据库
- hdu 5113(2014北京—搜索+剪枝)
- c++primer plus第十七章-输入和输出概述
- hdu 5113(2014北京—搜索+剪枝)
- 黑马程序员——OC语言基础---description方法介绍及重写
- 【整理】Android-Recovery Mode(recover模式详解)
- Oracle查询转换之视图合并
- ThreadPoolExecutor
- [译] --- GPUImage README.md
- 四校联训Round4心得体会
- Java面向对象(一)
- go语言 匿名变量