Find The Array

题:
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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值