传送门
题意:
给出一个数组a, 长度为n, \(n <= 1e5\),\(a[i], -1e4 <= a[i] <= 1e4\), 要求构造出一个b数组,使得\(\sum\limits_{i = 1}^{i = n}{a[i] * b[i]} = 0, \sum{abs(b[1 到 n])} <= 1e9\)
思路:
他要求最后的结果是0, 想想看0的构造方法,无非就是消消消,如果n是偶数的话,\(a[i], a[i + 1], 对应的构造为b[i] = -1 * a[i + 1], b[i + 1] = a[i]\), 这样就可以两两相消,如果是奇数的话,讨论前三个去两个相加不为0,\(a[1], a[2], a[3], 假设,a[1] + a[2] 不为0, 那么b[1] = a[3], b[2] = a[3], b[3] = -1 * (a[1] + a[2]),这样就可以想消\)
总结:
对于0要特别敏感,肯定是能够相互消除,然后就是考虑两两想消,三三想消
点击查看代码
#include <bits/stdc++.h>
#define endl '\n'
#define IOS ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
int T, n;
int main()
{
IOS; cin.tie(0), cout.tie(0);
cin >> T;
while (T--)
{
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
if (!(n % 2))
{
for (int i = 0; i < n; i += 2)
{
b[i] = -1 * a[i + 1];
b[i + 1] = 1 * a[i];
}
}
else
{
b[0] = a[2];
b[1] = a[2];
b[2] = -1 * (a[0] + a[1]);
for (int i = 3; i < n; i += 2)
{
b[i] = -1 * a[i + 1];
b[i + 1] = 1 * a[i];
}
}
for (int i = 0; i < b.size(); ++i)
cout << b[i] << " ";
cout << endl;
}
return 0;
}