====================================
Problem C. Erdős–Szekeres
对于 A【i】: 维护一个最小最大值序列 min【len】= x 即当前长度为len的递增序列中最小的最后一个值的下标x, 根据给出的A【i】,知道a[i]插入到位置 len,知道
a[ min[len-1] ] < a[i] < a[ min[len] ]。 于是我们(事先建了一个图,每个节点对应第i个数)在 min[len-1]节点到i, i到min[len]节点之间插入一个边; 并把min[len] 设为 i。
对于B【i】:同理逆推。
最后的这个图做一下拓扑,依次计算比a[i]小的元素个数 cnt[i],然后a[i]能够取到的最小值为当前可用的数里第cnt[i]+1个,并对所有比a[i]小的元素a[j]按照j下标从小到大的顺序依次从可用的数里取值. 注意可用的数一旦被取,就不能再用了。
====================================
https://code.csdn.net/snippets/512962
====================================
Nearly 80 years ago, two mathematicians, Paul Erdős and George Szekeres proved a famous result: X is guaranteed to have either an increasing subsequence of length at least sqrt(N) or a decreasing subsequence of length of at least sqrt(N). For example, (4, 5, 3, 7, 6, 2, 8, 1) has a decreasing subsequence of length 4: (5, 3, 2, 1).
I am teaching a combinatorics class, and I want to "prove" this theorem to my class by example. For every number X[i] in the sequence, I will calculate two values:
i | X[i] | A[i] | B[i] -----+--------+--------+-------- 0 | 4 | 1 | 4 1 | 5 | 2 | 4 2 | 3 | 1 | 3 3 | 7 | 3 | 4 4 | 6 | 3 | 3 5 | 2 | 1 | 2 6 | 8 | 4 | 2 7 | 1 | 1 | 1
I came up with a really interesting sequence to demonstrate this fact with, and I calculated A[i] and B[i] for every i, but then I forgot what my original sequence was. Given A[i] and B[i], can you help me reconstruct X?
X should consist of the numbers (1, 2, ..., N) in some order, and if there are multiple sequences possible, you should choose the one that is lexicographically smallest. This means that X[0] should be as small as possible, and if there are still multiple solutions, then X[1] should be as small as possible, and so on.
The first line of the input gives the number of test cases, T. T test cases follow, each consisting of three lines.
The first line of each test case contains a single integer N. The second line contains Npositive integers separated by spaces, representing A[0], A[1], ..., A[N-1]. The third line also contains N positive integers separated by spaces, representing B[0], B[1], ..., B[N-1].
For each test case, output one line containing "Case #x: ", followed by X[0], X[1], ... X[N-1] in order, and separated by spaces.
1 ≤ T ≤ 30.
It is guaranteed that there is at least one possible solution for X.
1 ≤ N ≤ 20.
1 ≤ N ≤ 2000.