1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

// Global variables
int nA, nB;
vector<int> A, B;
using it_t = decltype(A)::iterator;

int f(array<it_t, 2> begin, array<it_t, 2> end);

int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> nA >> nB;
    A.resize(nA);
    B.resize(nB);
    for (auto& x : A) cin >> x;
    for (auto& x : B) cin >> x;

    const int MOD = 1000000007;
    vector<int> ans(nA + nB);
    FOR (a_begin, A.begin(), A.end() - 1) {
        FOR (a_end, a_begin + 1, A.end()) {
            FOR (b_begin, B.begin(), B.end() - 1) {
                FOR (b_end, b_begin + 1, B.end()) {
                    int x = f({a_begin, b_begin}, {a_end, b_end}) - 1;
                    if (++ans[x] == MOD) ans[x] = 0;
                }
            }
        }
    }

    // Output
    for (const auto& x : ans) cout << x << " ";
    cout << "\n";
    return 0;
}

int
f(array<it_t, 2> begin, array<it_t, 2> end)
{
    int n[2] = {static_cast<int>(end[0] - begin[0]),
                static_cast<int>(end[1] - begin[1])};
    int best = n[0] + n[1];
    vector<int> perm(n[0] + n[1], 1);
    REP (i, n[0]) perm[i] = 0;
    do {
        int max_run = 1;
        bool up     = false;
        auto it     = begin;
        int last = *(it[perm[0]]++), run = 1;
        FOR (i, 1, ssize(perm) - 1) {
            const int& v = *(it[perm[i]]++);
            if ((v > last) == up)
                ++run;
            else
                run = 2, up = !up;
            last = v;
            if (run > max_run) {
                max_run = run;
                if (max_run > best) break;
            }
        }
        best = min(best, max_run);
    } while (next_permutation(perm.begin(), perm.end()));

    debug(best, vector<int>(begin[0], end[0]), vector<int>(begin[1], end[1]));
    return best;
}