2022年上组赛错题集——C. Find The Array

You are given an array [a1,a2,…,an][a1,a2,…,an] such that 1≤ai≤1091≤ai≤109. Let SS be the sum of all elements of the array aa.

Let's call an array bb of nn integers beautiful if:

  • 1≤bi≤1091≤bi≤109 for each ii from 11 to nn;
  • for every pair of adjacent integers from the array (bi,bi+1)(bi,bi+1), either bibi divides bi+1bi+1, or bi+1bi+1 divides bibi (or both);
  • 2∑i=1n|ai−bi|≤S2∑i=1n|ai−bi|≤S.

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 tt (1≤t≤10001≤t≤1000) — the number of test cases.

Each test case consists of two lines. The first line contains one integer nn (2≤n≤502≤n≤50).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

For each test case, print the beautiful array b1,b2,…,bnb1,b2,…,bn (1≤bi≤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

题目类型:构造

题目解析:1.不互质 2. 满足与a[i]的差的绝对值的累加*2 <= a[i]的累加和。

解题思路:我看了好多人 都是 用二进制2^k的性质 和 二分逼近的方法做的, 还有直接 用1 a[i] 1 a[i+2]…… 和  a[i] 1 a[i+2] 1 ……中的较小值来实现的。

这里给一个 第二种的 一个优化,首先 a[i] % 1 == 0 必然成立, 为缩小差距,将 数值为 1 的点 在择优的情况下 , 改为 前后非1值(也可能是1)的元素的最小公约数,如果这个最小公约数与a[i]的的绝对值 比 1 与 a[i] 的 绝对值更小。

AC代码:

#include <bits/stdc++.h>
#define rep(x, a, b) for(int x = a; x <= b; x++)
#define pre(x, a, b) for(int x = b; x >= a; x--)
#define ll long long
#define int unsigned long long
#define PII pair<int, int> 
#define inf 0x3f3f3f3f
using namespace std;
int dx[]={0, 1, 0, 1}, dy[]= {0, 0, 1, 1};
const int N = 60;
int a[N], b[N], c[N];
void solve()
{
	int n;
	cin>>n;
	double sum = 0, sum2 = 0, sum3 = 0;
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	memset(c, 0, sizeof(c));
	for(int i = 1; i<= n; i++)
	{
		cin>>a[i]; sum += a[i];
		if(i % 2) b[i] = 1, c[i] = a[i];
		else b[i] = a[i], c[i] = 1;
	}
	for(int i = 2; i<= n - 1; i++)
	{
		if(b[i] == 1)
		{
			double temp = (double)__gcd(b[i-1], b[i + 1]);
			if((abs(temp - a[i]*1.0)) < abs(a[i]*1.0- b[i])) b[i] = (int)temp;
		}
	}
	for(int i = 3; i<= n - 1; i++)
	{
		if(c[i] == 1)
		{
			double temp = (double)__gcd(c[i-1], c[i + 1]);
			if((abs(temp - a[i]*1.0)) < abs(a[i]*1.0- c[i])) c[i] =(int) temp;
		}
	}
	for(int i = 1; i<= n; i++)
		sum2 += abs(1.0*a[i] - b[i]),
		sum3 += abs(1.0*a[i] - c[i]);

	if(sum2 < sum3) for(int i = 1; i<=n; i++) cout<<b[i]<<' ';
	else  for(int i = 1; i<= n; i++) cout<<c[i]<<' ';
	cout<<endl;
}
signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
   int t = 1;
   bool isc = true;
   if(isc) cin>>t;
   while(t -- )
   {
        solve();
  }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值