首页 > 其他 > 详细

Educational Codeforces Round 100 (Rated for Div. 2)B. Find The Array(构造)

时间:2020-12-18 10:17:36      阅读:23      评论:0      收藏:0      [点我收藏+]

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:

  • 1≤????≤1091≤bi≤109 for each ??i from 11 to ??n;
  • for every pair of adjacent integers from the array (????,????+1)(bi,bi+1), either ????bi divides ????+1bi+1, or ????+1bi+1 divides ????bi (or both);
  • 2∑??=1??|?????????|≤??2∑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 ??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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!