题意 :
- 给两个长为n的序列,球构造一组操作次数不超过n的方案使第一个序列变为第二个序列
- 每次操作选定一个区间使其升序或降序
思路 :
- 需要n次,一次确定一个位置
- 先将序列a整个递增排序;然后从后往前枚举序列b,记录i为r,每次找到b[i]在a中的位置为l,如果a[l] >a[r],则将这段递增,反之,则递减
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510;
int a[N], b[N];
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ ) cin >> b[i];
cout << n << endl << 1 << ' ' << n << ' ' << "I" << endl;
sort(a + 1, a + n + 1);
for (int i = n; i >= 2; i -- )
{
int t = b[i];
int l, r = i;
for (int j = i; j >= 1; j -- )
if (a[j] == t)
{
l = j;
break;
}
if (a[l] > a[r])
{
sort(a + l, a + r + 1);
cout << l << ' ' << r << " I" << endl;
}
else
{
sort(a + l, a + r + 1, cmp);
cout << l << ' ' << r << " D" << endl;
}
}
return 0;
}