#include <ios>
#include <functional>
#include <cassert>
#include <vector>
#define REP(i, n) for(int i=0; i<(n); ++i)
#define SREP(n) REP(_ ## n, n)
#define FOR(i, p, n) for(int i=(p); i<=(n); ++i)
#define RFOR(i, p, n) for(int i=(p); i>=(n); --i)
#define pb push_back
#define eb emplace_back
#define C const
#define V std::vector
#define F std::function
#define R std::ranges
#define RV std::ranges::views
#define sz(A) int(A.size())
#define all(A) A.begin(), A.end()
#define rall(A) A.rbegin(), A.rend()
typedef long long ll;
typedef long double ld;
typedef V <int> vi;
typedef V <vi> vvi;
typedef V <bool> vb;
typedef V <char> vc;
typedef V <ll> vll;
typedef const int ci;
typedef const ll cll;
int I(){
int z;
while ((z=getchar_unlocked())<'0'||z>'9');
int r=z-'0';
while ((z=getchar_unlocked())>='0'&&z<='9')
r=r*10+z-'0';
return r;
}
void M(int &i, ci j){
if (i>j)
i=j;
}
ci MOD=1e9+7;
int main(){
ci n=I(),m=I();
vi A(n),B(m);
REP(i, n)
A[i]=I();
REP(i, m)
B[i]=I();
vi wyn(n+m+1);
REP(p1, n)
REP(p2, m){
vi v1={0},v2={0};
FOR(p, p1, n-1)
v1.eb(A[p]);
FOR(p, p2, m-1)
v2.eb(B[p]);
ci n1=sz(v1),n2=sz(v2);
// dp[czy2kon][czyostros][dł ost prz][k1][k2]=min stab
ci dlcap=n-p1+m-p2;
ci inf=1e9;
V <V <V <V <V <int>>>>> dp(2, V(2, V(dlcap+1, V(n1, vi(n2, inf)))));
dp[0][0][0][0][0]=0;
REP(k1, n1)
REP(k2, n2){
int min=inf;
REP(czy2kon, 2){
ci pop=czy2kon ? v2[k2] : v1[k1];
REP(czyostros, 2)
REP(dl, dlcap+1){
ci l=dp[czy2kon][czyostros][dl][k1][k2];
if (l==inf)
continue;
min=std::min(min, l);
if (k1+1<n1){
C bool rel=v1[k1+1]>pop;
C bool czykont=rel==czyostros;
ci ndl=czykont ? dl+1 : (k1+k2 ? 2 : 1);
ci nwyn=std::max(l, ndl);
M(dp[0][rel][ndl][k1+1][k2], nwyn);
}
if (k2+1<n2){
C bool rel=v2[k2+1]>pop;
C bool czykont=rel==czyostros;
ci ndl=czykont ? dl+1 : (k1+k2 ? 2 : 1);
ci nwyn=std::max(l, ndl);
M(dp[1][rel][ndl][k1][k2+1], nwyn);
}
}
}
if (k1&&k2)
++wyn[min];
}
}
FOR(i, 1, n+m)
printf("%d ", wyn[i]%MOD);
printf("\n");
}
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 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <ios> #include <functional> #include <cassert> #include <vector> #define REP(i, n) for(int i=0; i<(n); ++i) #define SREP(n) REP(_ ## n, n) #define FOR(i, p, n) for(int i=(p); i<=(n); ++i) #define RFOR(i, p, n) for(int i=(p); i>=(n); --i) #define pb push_back #define eb emplace_back #define C const #define V std::vector #define F std::function #define R std::ranges #define RV std::ranges::views #define sz(A) int(A.size()) #define all(A) A.begin(), A.end() #define rall(A) A.rbegin(), A.rend() typedef long long ll; typedef long double ld; typedef V <int> vi; typedef V <vi> vvi; typedef V <bool> vb; typedef V <char> vc; typedef V <ll> vll; typedef const int ci; typedef const ll cll; int I(){ int z; while ((z=getchar_unlocked())<'0'||z>'9'); int r=z-'0'; while ((z=getchar_unlocked())>='0'&&z<='9') r=r*10+z-'0'; return r; } void M(int &i, ci j){ if (i>j) i=j; } ci MOD=1e9+7; int main(){ ci n=I(),m=I(); vi A(n),B(m); REP(i, n) A[i]=I(); REP(i, m) B[i]=I(); vi wyn(n+m+1); REP(p1, n) REP(p2, m){ vi v1={0},v2={0}; FOR(p, p1, n-1) v1.eb(A[p]); FOR(p, p2, m-1) v2.eb(B[p]); ci n1=sz(v1),n2=sz(v2); // dp[czy2kon][czyostros][dł ost prz][k1][k2]=min stab ci dlcap=n-p1+m-p2; ci inf=1e9; V <V <V <V <V <int>>>>> dp(2, V(2, V(dlcap+1, V(n1, vi(n2, inf))))); dp[0][0][0][0][0]=0; REP(k1, n1) REP(k2, n2){ int min=inf; REP(czy2kon, 2){ ci pop=czy2kon ? v2[k2] : v1[k1]; REP(czyostros, 2) REP(dl, dlcap+1){ ci l=dp[czy2kon][czyostros][dl][k1][k2]; if (l==inf) continue; min=std::min(min, l); if (k1+1<n1){ C bool rel=v1[k1+1]>pop; C bool czykont=rel==czyostros; ci ndl=czykont ? dl+1 : (k1+k2 ? 2 : 1); ci nwyn=std::max(l, ndl); M(dp[0][rel][ndl][k1+1][k2], nwyn); } if (k2+1<n2){ C bool rel=v2[k2+1]>pop; C bool czykont=rel==czyostros; ci ndl=czykont ? dl+1 : (k1+k2 ? 2 : 1); ci nwyn=std::max(l, ndl); M(dp[1][rel][ndl][k1][k2+1], nwyn); } } } if (k1&&k2) ++wyn[min]; } } FOR(i, 1, n+m) printf("%d ", wyn[i]%MOD); printf("\n"); } |
English