A. AquaMoon and Two Arrays
Description
AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays a and b, both consist of n non-negative
integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero):
She chooses two indices i and j (1≤i,j≤n), then decreases the i-th element of array a by 1, and increases the j-th element of
array a by 1. The resulting values at i-th and j-th index of array a are ai−1 and aj+1, respectively. Each element of array a must
be non-negative after each operation. If i=j this operation doesn’t change the array a.
AquaMoon wants to make some operations to make arrays a and b equal. Two arrays a and b are considered equal if and only if ai=bi
for all 1≤i≤n.
Help AquaMoon to find a sequence of operations that will solve her problem or find, that it is impossible to make arrays a and b equal.
Please note, that you don’t have to minimize the number of operations.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100).
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤100). The sum of all ai does not exceed 100.
The third line of each test case contains n integers b1,b2,…,bn (0≤bi≤100). The sum of all bi does not exceed 100.
Output
For each test case print “-1” on the only line if it is impossible to make two arrays equal with some sequence of operations.
Otherwise, print an integer m (0≤m≤100) in the first line — the number of operations. Then print m lines, each line consists of two
integers i and j — the indices you choose for the operation.
It can be proven that if it is possible to make two arrays equal with some sequence of operations, there exists a sequence with m≤100.
If there are multiple possible solutions, you can print any.
Example
input
4
4
1 2 3 4
3 1 2 4
2
1 3
2 1
1
0
0
5
4 3 2 1 0
0 1 2 3 4
output
2
2 1
3 1
-1
0
6
1 4
1 4
1 5
1 5
2 5
2 5
Note
In the first example, we do the following operations:
i=2, j=1: [1,2,3,4]→[2,1,3,4];
i=3, j=1: [2,1,3,4]→[3,1,2,4];
In the second example, it’s impossible to make two arrays equal.
题目大意:
给定数组a和目标数组b,可以选中任意的i, j使ai + 1且aj - 1,最后要让数组a和数组b相等,操作完的元素不能是负数。如果不可能就输出-1;如果存在方案就输出任意一种方案,方案输出的格式是:操作数以及每一步的i和j。
题解:
因为每一次改变都会有元素加一和元素减一,所以首先要统计原始数组和目标数组每一个相同位置元素的差值,如果所有的差值加起来不为0,说明是无法实现的。接着按照将正数差值和负数差值按1抵消并输出i和j即可。
AC代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
ll a[N], b[N];
int main() {
ios::sync_with_stdio(false);
// freopen("out.txt","w",stdout);
ll t;
cin >> t;
while (t --) {
ll n;
cin >> n;
for (ll i = 0; i < n; i ++) cin >> a[i]; // 输入
for (ll i = 0; i < n; i ++) cin >> b[i];
ll suma = 0, sumb = 0;
vector<pair<ll, ll> > vta; // 位置下标以及正数差值
vector<pair<ll, ll> > vtb; // 位置下标以及负数差值
for (ll i = 0; i < n; i ++) {
if (b[i] > a[i]) { // 正数差值
suma += b[i] - a[i];
vta.push_back(make_pair(i, b[i] - a[i]));
}
else if (b[i] < a[i]) {
sumb += a[i] - b[i];
vtb.push_back(make_pair(i, a[i] - b[i]));
}
}
// cout << "-----------------------------------\n";
// for (ll i = 0; i < vta.size(); i ++) {
// cout << vta[i].first << ' ' << vta[i].second << '\n';
// }
// cout << "-----------------------------------\n";
// for (ll i = 0; i < vtb.size(); i ++) {
// cout << vtb[i].first << ' ' << vtb[i].second << '\n';
// }
// cout << "-----------------------------------\n";
ll pa = 0, pb = 0;
if (sumb != suma) cout << "-1\n"; // 差值和不为0
else {
cout << suma << '\n';
while (suma --) { // 正数差值和负数差值一一抵消,步骤总数就是正数差值或者负数差值的和
cout << vtb[pb].first + 1 << ' ' << vta[pa].first + 1 << '\n';
vta[pa].second --;
if (vta[pa].second <= 0) pa ++; // 一个位置的差值抵消完了就换下一个
vtb[pb].second --;
if (vtb[pb].second <= 0) pb ++;
}
}
}
return 0;
}