题:
You are given an array [𝑎1,𝑎2,…,𝑎𝑛] such that 1≤𝑎𝑖≤109. Let 𝑆 be the sum of all elements of the array 𝑎.
Let’s call an array 𝑏 of 𝑛 integers beautiful if:
1≤𝑏𝑖≤109 for each 𝑖 from 1 to 𝑛;
for every pair of adjacent integers from the array (𝑏𝑖,𝑏𝑖+1), either 𝑏𝑖 divides 𝑏𝑖+1, or 𝑏𝑖+1 divides 𝑏𝑖 (or both);
2∑𝑖=1𝑛|𝑎𝑖−𝑏𝑖|≤𝑆.
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 𝑡 (1≤𝑡≤1000) — the number of test cases.
Each test case consists of two lines. The first line contains one integer 𝑛 (2≤𝑛≤50).
The second line contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤10^9).
Output
For each test case, print the beautiful array 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤10^9) 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
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
output
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3
思路:
构造题,关键就在于1,相邻两个数之间填1是满足题意的,
因为两个数组对应数的差值绝对值是要小于两倍a数组和的,
所以有两种策略:
b数组奇数位置和a数组一样,偶数位置填1
b数组偶数位置和a数组一样,奇数位置填1
因为a数组奇数位置和与偶数位置和一定有一个大于等于数组一半,所以两种策略中一定有一个满足条件。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
long a[55],b1[55],b2[55];
long long s=0,s1=0;
for(int i=0;i<n;++i)
{
cin>>a[i];
s+=a[i];
}
for(int i=0;i<n;++i)
{
if(i%2==0)
b1[i]=1;
else
b1[i]=a[i];
}
for(int i=0;i<n;++i)
{
s1+=a[i]-b1[i];
}
if(2*s1>s)
{
for(int i=0;i<n;++i)
{
if(i%2!=0)
b2[i]=1;
else
b2[i]=a[i];
}
for(int i=0;i<n;++i)
cout<<b2[i]<<" ";
cout<<endl;
}
else
{
for(int i=0;i<n;++i)
cout<<b1[i]<<" ";
cout<<endl;
}
}
return 0;
}