Codeforces 1463 B. Find The Array

2024-04-15 23:18
文章标签 codeforces find array 1463

本文主要是介绍Codeforces 1463 B. 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≤𝑎𝑖≤109).

Output
For each test case, print the beautiful array 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤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
inputCopy
4
5
1 2 3 4 5
2
4 6
2
1 1000000000
6
3 4 8 1 2 3
outputCopy
3 3 3 3 3
3 6
1 1000000000
4 4 8 1 3 3

题意:
给你 a a a数组,要求你构造 b b b数组满足这三个条件
在这里插入图片描述
思路:
构造题,关键就在于1,相邻两个数之间填1是满足题意的,
因为两个数组对应数的差值绝对值是要小于两倍 a a a数组和的,
所以又两种策略:

  1. b b b数组奇数位置和 a a a数组一样,偶数位置填1
  2. b b b数组偶数位置和 a a a数组一样,奇数位置填1

因为 a a a数组奇数位置和与偶数位置和一定有一个大于等于数组一半,所以两种策略中一定有一个满足条件。

#include <algorithm>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>using namespace std;
typedef long long ll;
const int maxn = 1e3 + 7;
int a[55];
int main() {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);ll sum1 = 0,sum2 = 0;for(int i = 1;i <= n;i++) {scanf("%d",&a[i]);if(i & 1) sum1 += a[i];else sum2 += a[i];}if(sum1 >= sum2) {for(int i = 1;i <= n;i++) {if(i & 1) printf("%d ",a[i]);else printf("1 ");}} else {for(int i = 1;i <= n;i++) {if(!(i & 1)) printf("%d ",a[i]);else printf("1 ");}}printf("\n");}return 0;
}

这篇关于Codeforces 1463 B. Find The Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907200

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this