

正确代码
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> pii;
const int N = 1e6 + 100;
const int inf = LLONG_MAX;
const double eps = 1e-6;
const int mod = 1e9+7;
struct xx {
char c;
int l, r;
};
void miaojiachun() {
int n;
cin >> n;
vector<int> a(n + 1);
vector<int> b(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
vector<int> _b;
int las = -1;
for (int i = 1; i <= n; i++) {
if (b[i] != las) {
_b.push_back(b[i]);
las = b[i];
}
}
vector<int> pos(_b.size() + 1);//记录每个元素在a中的位置。
int j = 0;
for (int i = 1; i <= n and j < _b.size(); i++) {
if (a[i] == _b[j]) {
pos[j + 1] = i;
j++;
}
}
if (j != _b.size()) {
cout << "NO" << endl;
return;
}
// 分个b数组为连续相同元素的区间。
vector<pair<int, int>> sg;
for (int i = 1; i <= n;) {
int str = i;
while (i <= n and b[i] == b[str]) {
i++;
}
sg.emplace_back(str, i - 1);
}
vector<pair<char, pii>> op;
for (int i = 0; i < sg.size(); i++) {
int l = sg[i].first;
int r = sg[i].second;
int pos_a = pos[i + 1];
if (l < pos_a) {
op.emplace_back('L', make_pair(l - 1, pos_a - 1));
}
}
for (int i = sg.size() - 1; i >= 0; i--) {
int l = sg[i].first;
int r = sg[i].second;
int pos_a = pos[i + 1];
if (r > pos_a) {
op.emplace_back('R', make_pair(pos_a - 1, r - 1));
}
}
cout << "YES" << endl;
cout << op.size() << endl;
for (auto c : op) {
cout << c.first << " " << c.second.first << " " << c.second.second << endl;
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int ING = 1;
// cin >> ING;
while (ING--) {
miaojiachun();
}
return 0;
}
/*
YES
2
R 2 3
R 0 1
*/
错误代码
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> pii;
const int N = 1e6 + 100;
const int inf = LLONG_MAX;
const double eps = 1e-6;
const int mod = 1e9+7;
struct xx {
char c;
int l, r;
};
void miaojiachun() {
int n;
cin >> n;
vector<int> a(n + 1);
vector<int> b(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
vector<int> _b;
int las = -1;
for (int i = 1; i <= n; i++) {
if (b[i] != las) {
_b.push_back(b[i]);
las = b[i];
}
}
vector<int> pos(_b.size() + 1);//记录每个元素在a中的位置。
int j = 0;
for (int i = 1; i <= n and j < _b.size(); i++) {
if (a[i] == _b[j]) {
pos[j + 1] = i;
j++;
}
}
if (j != _b.size()) {
cout << "NO" << endl;
return;
}
// 分个b数组为连续相同元素的区间。
vector<pair<int, int>> sg;
for (int i = 1; i <= n;) {
int str = i;
while (i <= n and b[i] == b[str]) {
i++;
}
sg.emplace_back(str, i - 1);
}
vector<pair<char, pii>> op;
for (int i = 0; i < sg.size(); i++) {
int l = sg[i].first;
int r = sg[i].second;
int pos_a = pos[i + 1];
if (l < pos_a) {
op.emplace_back('L', make_pair(l - 1, pos_a - 1));
}
if (r > pos_a) {
op.emplace_back('R', make_pair(pos_a - 1, r - 1));
}
}
cout << "YES" << endl;
cout << op.size() << endl;
for (auto c : op) {
cout << c.first << " " << c.second.first << " " << c.second.second << endl;
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int ING = 1;
// cin >> ING;
while (ING--) {
miaojiachun();
}
return 0;
}
/*
YES
2
R 2 3
R 0 1
*/
