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 97 98 99 100 101 102 103 104 105 106 107 108
| #include <bits/stdc++.h> using namespace std; const int maxn=100000+10; struct ddd { int x,l,r; }; ddd e[maxn]; int a[maxn],b[maxn],pos[maxn]; int n,cnt=0,now=1,ans=0; char ch[maxn*2]; int L[maxn*2],R[maxn*2]; void add(char c, int l, int r) { if(l>=r) return; ch[ans]=c; L[ans]=l; R[ans]=r; ++ans; } int main() { scanf("%d",&n); for(int i=1; i<=n; ++i) scanf("%d",&a[i]); for(int i=1; i<=n; ++i) scanf("%d",&b[i]); now=1; for(int i = 1;i <= n;i++) { while(now <= n && a[now] != b[i]) ++now; if(now > n) { printf("-1\n"); return 0; } pos[i] = now; } now=1; cnt=0; for(int i = 1;i <= n;i++) { if(pos[i] == pos[i+1]) continue; e[cnt++] = (ddd){pos[i], now, i}; now = i + 1; } ans=0; for(int i=cnt-1; i>=0; --i) { if(e[i].x >= e[i].l) continue; if(a[e[i].x] >= a[e[i].x+1]) { add('m', e[i].x+1, e[i].r); add('M', e[i].x, e[i].r); } else { add('M', e[i].x+1, e[i].r); add('m', e[i].x, e[i].r); } } for(int i=0; i<cnt; ++i) { if(e[i].x <= e[i].r) continue; if(a[e[i].x] <= a[e[i].x-1]) { add('M', e[i].l, e[i].x-1); add('m', e[i].l, e[i].x); } else { add('m', e[i].l, e[i].x-1); add('M', e[i].l, e[i].x); } } for(int i=0; i<cnt; ++i) { if(e[i].l > e[i].x || e[i].r < e[i].x) continue; if(e[i].x > e[i].l) { if(a[e[i].x] >= a[e[i].x-1]) { add('m', e[i].l, e[i].x-1); add('M', e[i].l, e[i].x); } else { add('M', e[i].l, e[i].x-1); add('m', e[i].l, e[i].x); } } if(e[i].x < e[i].r) { if(a[e[i].x] >= a[e[i].x+1]) { add('m', e[i].x+1, e[i].r); add('M', e[i].x, e[i].r); } else { add('M', e[i].x+1, e[i].r); add('m', e[i].x, e[i].r); } } } printf("%d\n",ans); for(int i=0; i<ans; ++i) printf("%c %d %d\n",ch[i],L[i],R[i]); return 0; }
|