You are given an array [??1,??2,…,????][a1,a2,…,an] such that 1≤????≤1091≤ai≤109. Let ??S be the sum of all elements of the array ??a.
Let‘s call an array ??b of ??n integers beautiful if:
Your task is to find any beautiful array. It can be shown that at least one beautiful array always exists.
Input
The first line contains one integer ??t (1≤??≤10001≤t≤1000) — the number of test cases.
Each test case consists of two lines. The first line contains one integer ??n (2≤??≤502≤n≤50).
The second line contains ??n integers ??1,??2,…,????a1,a2,…,an (1≤????≤1091≤ai≤109).
Output
For each test case, print the beautiful array ??1,??2,…,????b1,b2,…,bn (1≤????≤1091≤bi≤109) on a separate line. It can be shown that at least one beautiful array exists under these circumstances. If there are multiple answers, print any of them.
Example
input
Copy
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
output
Copy
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
构造题。一开始没有头绪甚至在瞎吉尔写,但是发现“二倍的差的绝对值之和小于等于S”,这里面的2很特殊。于是考虑按照奇偶构造。另一个条件是说相邻两个数满足一个数能被另一个数整除,比较特殊一点的就是1,因为1能够整除任意一个数。于是考虑如下构造:首先对于b[i]如果i是奇数就令b[i]为1,否则令b[i] = a[i]。对于这样构造出来的数列判断一下是否满足绝对值那个条件,满足的话直接输出,不满足的话就让i是偶数b[i]为1....反着构造一次。这两种情况一定有一种是满足的,因为把n个数分成两部分,一定有一部分大于等于n / 2。
#include <iostream>
using namespace std;
int n, a[55],low[55], b[55];
int main()
{freopen("data.txt", "r", stdin);
int t;
cin >> t;
while(t--)
{
long long sum = 0;
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
sum += 1ll * a[i];
}
long long tmp = 0;
for(int i = 1; i <= n; i++)
{
if(i & 1)
{
b[i] = 1;
tmp += 1ll * (a[i] - b[i]);
}
else b[i] = a[i];
}
if(tmp * 2 <= sum)
{
for(int i = 1; i <= n; i++) cout << b[i] << ‘ ‘;
cout << endl;
continue;
}
for(int i = 1; i <= n; i++)
{
if(!(i & 1))
{
b[i] = 1;
tmp += 1ll * (a[i] - b[i]);
}
else b[i] = a[i];
}
for(int i = 1; i <= n; i++) cout << b[i] << ‘ ‘;
cout << endl;
}
return 0;
}
Educational Codeforces Round 100 (Rated for Div. 2)B. Find The Array(构造)
原文:https://www.cnblogs.com/lipoicyclic/p/14153160.html