https://codeforces.ml/contest/1546/problem/0
题意:
Meaning:
给你两个数组a和b,a中可以选取两个数,令其中一个数减一,另一个数加一。如果可以通过m次这样的操作让a和b数组完全相同,则输出输出m并输出每次操作选取的两个数的序号,如果不能就输出-1
You are given two arrays, a and b. You could choose two numbers in array a, and make one minus one and add one to the other. If you could make the arrays equal after m operations, output the number m, and output all of the operations with the i and j you choose at any ops.
分析:
Analysis:
遍历每个位置,对比ai和bi,如果ai比较大说明ai要增加增加嘛,小的话就要减少,如果增加的数和减少的数一样的话,比如第一个位置增加3,第二个位置减少1,第三个位置减少2,没了,那就可以通过三次操作完成,如果不相等那输出-1就好啦
Traverse each position and compare AI and Bi. If AI is larger, it means that AI needs to be increased and decreased otherwise. If the increased number is the same as the decreased number, for example, the first position increases by 3, the second position decreases by 1, and the third position decreases by 2. If it’s gone, it can be completed by three operations. If it’s not equal, it’s better to output – 1
Code:
#include<bits/stdc++.h>
using namespace std;
int t, n;
int main()
{
cin >> t;
while(t --)
{
cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; i ++) cin >> a[i];
for(int i = 0; i < n; i ++) cin >> b[i];
vector<int> up, down;
for(int i = 0; i < n; i ++)
if(a[i] < b[i])
for(int j = 0; j < b[i] - a[i]; j ++) up.push_back(i + 1);
else if(a[i] > b[i])
for(int j = 0; j < a[i] - b[i]; j ++) down.push_back(i + 1);
if(up.size() == down.size())
{
cout << up.size() << endl;
for(int i = 0; i < up.size(); i ++) cout << down[i] << ' ' << up[i] << endl;
}
else cout << -1 << endl;
}
}