Bridging signals
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.
Two signals cross if and only if the straight lines connecting the two ports of each pair do.
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
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题目意思:左边是1-p个数按1,2,。。。p的顺序排列,右边是左边的映射,即右边的数有唯一的数与左边对应(数学中的函数)。输入右边的映射数,输出做多的数对个数,使它们不会相交(上面的图,有线交叉就是相交了)。思路:题目看起来挺复杂的,我们抽象出来就可以知道,这题实质上是要我们求一个数列的最大递增子序列的长度。注意点:1.输入数据挺大的,用cin的话会超时,我们用cin还需外加std::ios::sync_with_stdio(false); 来解除和scanf和printf的绑定(这也是我第一次做题遇到cin超时问题,学习了)2.写LIS必须用的是O(nlogn)的算法,O(N*N)会超时。代码:#include <iostream> #include <vector> #include <algorithm> using namespace std; int a[40050]; int LIS(int *a, int len) { vector<int>mavx; mavx.push_back(a[1]); for (int i = 1; i <= len; i++) { if (a[i] > *mavx.rbegin()) { mavx.push_back(a[i]); } else { *(lower_bound(mavx.begin(),mavx.end(),a[i])) = a[i]; } } return mavx.size(); } int main() { int n,p,i; std::ios::sync_with_stdio(false); cin >> n; if (!n) { return 0; } while(n--) { cin >> p; if(!p) { return 0; } for (i = 1; i <= p; i++) { cin >> a[i]; } cout << LIS(a,p) << endl; } return 0; }
回来再补一下:
又回去提交,虽然过了,但是发现速度很慢,看了一个最快的代码,思路跟他的差不多,只不过我用的是容器,他用的是数组,难道容器比数组慢那么多??实验了好几次,才发现是cin的问题,即使解除了绑定,还是慢啊,最后把所有的cin改成scanf.看来以后得用scanf了=。=
修改代码: