[构造记录]CF1854A1&2 Dual
题意:
给你一个
一次操作定义为选取两个
要求构造一种方案,使得最后序列单调不降,且操作次数不超过
思路:
先观察。
如果
这样最后
我们只需要知道
由于
所以两种方法中一定有一种使得最后操作次数不超过
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
int n, a[25] = {0};
int mx_abs = 0, idx_abs = 0;
vector<pair<int, int> > op_pos, op_neg, ans;
bool all_pos() {
for (int i = 1; i <= n; i++)
if (a[i] < 0)
return false;
for (int i = 1; i < n; i++)
op_pos.push_back(make_pair(i + 1, i));
return true;
}
bool all_neg() {
for (int i = 1; i <= n; i++)
if (a[i] > 0)
return false;
for (int i = n - 1; i >= 1; i--)
op_neg.push_back(make_pair(i, i + 1));
return true;
}
void turn_to_pos() {
int idx_pos = idx_abs;
if (a[idx_abs] <= 0) {
for (int i = 1; i <= n; i++)
if (a[i] > 0) {
int x = a[i];
while (x < 20) {
op_pos.push_back(make_pair(i, i));
x *= 2;
}
idx_pos = i;
break;
}
}
for (int i = 1; i <= n; i++)
if (a[i] < 0)
op_pos.push_back(make_pair(i, idx_pos));
for (int i = 2; i <= n; i++)
op_pos.push_back(make_pair(i, i - 1));
}
void turn_to_neg() {
int idx_neg = idx_abs;
if (a[idx_abs] >= 0) {
for (int i = 1; i <= n; i++)
if (a[i] < 0) {
int x = a[i];
while (x > -20) {
op_neg.push_back(make_pair(i, i));
x *= 2;
}
idx_neg = i;
break;
}
}
for (int i = 1; i <= n; i++)
if (a[i] > 0)
op_neg.push_back(make_pair(i, idx_neg));
for (int i = n - 1; i >= 1; i--)
op_neg.push_back(make_pair(i, i + 1));
}
void slv() {
cin >> n;
op_pos = op_neg = ans = vector<pair<int, int> >();
mx_abs = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (mx_abs < abs(a[i])) {
mx_abs = abs(a[i]);
idx_abs = i;
}
}
if (all_pos())
ans = op_pos;
else if (all_neg())
ans = op_neg;
else {
turn_to_pos();
turn_to_neg();
if (op_pos.size() < op_neg.size())
ans = op_pos;
else
ans = op_neg;
}
cout << (int)ans.size() << endl;
for (auto i: ans)
cout << i.first << " " << i.second << endl;
}
int main() {
int T;
cin >> T;
while (T--)
slv();
return 0;
}