题目
思路:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
const int maxn = 1e6 + 5, inf = 1e18, maxm = 4e4 + 5, mod = 1e9 + 7, N = 1e6;
int a[maxn], b[maxn], d[maxn];
bool vis[maxn];
int n, m;
string s;
struct Node{
int val, id;
bool operator<(const Node &u)const{
return val < u.val;
}
};
vector<pair<int, int>> ans;
bool work(int flag){
ans.clear();
for(int i = 1; i <= n; i++){
a[i] = b[i];
}
int mx = 0;
int x = 0, y = 0;//最大的正数的位置和最大的负数的位置
for(int i = 1; i <= n; i++){
mx = max(mx, abs(a[i]));
if(a[i] > a[x]){
x = i;
}
if(a[i] < a[y]){
y = i;
}
}
if(flag){
if(!x) return 0;
while(abs(a[x]) < mx){
a[x] += a[x];
ans.pb({x, x});
}
for(int i = 1; i <= n; i++){
if(a[i] < 0){
// a[i] += a[x];
ans.pb({i, x});
}
}
for(int i = 2; i <= n; i++){
ans.pb({i, i - 1});
}
}
else{
if(!y) return 0;
while(abs(a[y]) < mx){
a[y] += a[y];
ans.pb({y, y});
}
for(int i = 1; i <= n; i++){
if(a[i] > 0){
// a[i] += a[y];
ans.pb({i, y});
}
}
for(int i = n - 1; i >= 1; i--){
ans.pb({i, i + 1});
}
}
return ans.size() <= 31;
}
void solve(){
int res = 0;
int q, k;
cin >> n;
ans.clear();
for(int i = 1; i <= n; i++){
cin >> a[i];
b[i] = a[i];
}
if(count(a + 1, a + n + 1, 0) == n || is_sorted(a + 1, a + n + 1)){//全是0要特判
cout << 0 << '\n';
return;
}
for(int i = 0; i < 2; i++){
if(work(i)){
cout << ans.size() << '\n';
for(auto [l, r] : ans){
cout << l << ' ' << r << '\n';
}
return;
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}