当前位置:网站首页>Educational codeforces round 100 (rated for Div. 2) B. find the array solution
Educational codeforces round 100 (rated for Div. 2) B. find the array solution
2022-07-24 16:22:00 【Is_ TuTouYa】
Front row thanks @Leonard.7 The idea given by the boss qwq
Without the help of the boss, I have this batch of vegetables A Don't drop this question qwq
The original title is
You are given an array [a1,a2,…,an] such that 1≤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≤bi≤109 for each i from 1 to n;
for every pair of adjacent integers from the array (bi,bi+1), either bi divides bi+1, or bi+1 divides bi (or both);
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≤t≤1000) — the number of test cases.
Each test case consists of two lines. The first line contains one integer n (2≤n≤50).
The second line contains n integers a1,a2,…,an (1≤ai≤109).
Output
For each test case, print the beautiful array b1,b2,…,bn (1≤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.
A simple explanation
Give an array a, The sum of each number of its array is S, Let you find an array b Meet the following conditions :
- b[i] ∈ [1,1e9]
- 2*∑|a[i]-b[i]| <= S
- b[i] Can be b[i+1] Divide or be divided
Here is a solution that basically presses the maximum .
In array 1,3,2,5,4 give an example , First, separate the array with odd and even numbers , namely
1 ,2, 4 and 3,5
The sum of one of the two separate arrays must be greater than S/2.
In this case, that is 1,2,4.
So if we want to ask for an array b, It's an array b The number in is 1,x,2,x,4.
The obvious thing is x=1. This question is proved .
The following is a very inconvenient code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
const int N = 100;
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 0;i < n;i++){
int x,a[N],b[N];
long long sum = 0,tot = 0;
cin >> x;
for(int j = 0;j < x;j++){
cin >> a[j];
tot+= a[j]; // seek S
if(j % 2==0) b[j] = 1;
else b[j] = a[j];
sum+=abs(a[j]-b[j]); // Calculation ∑|a[i]-b[i]|
}
if(2*sum < tot) // Judge from two situations , It's troublesome to write double here
for(int j = 0;j < x;j++)cout << b[j] << " ";
else{
// The second case
for(int j = 0;j < x;j++){
if(j%2!=0) b[j] = 1;
else b[j] = a[j];
}
for(int j = 0;j < x;j++)cout << b[j] << " ";
}
cout << endl;
}
return 0;
}
边栏推荐
- 【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
- Getting started with OpenMP
- 狗牙根植物介绍
- Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
- Yolov3 trains its own data set
- After data management, the quality is still poor
- MySQL之知识点(十二)
- 287 finding duplicates
- 自适应设计和响应式设计
- 解决Eureka默认缓存配置导致时效性问题
猜你喜欢

Servlet框架(servlet+jsp)+Mysql实现的增删改查+分页(功能包学生信息录入、学生信息增删改查、分页等)
![Leetcode:162. looking for peak [two points looking for peak]](/img/77/64b7c9bf1aebc2a0ab82218ddfff62.png)
Leetcode:162. looking for peak [two points looking for peak]

How to prevent XSS in PHP

Yolov6 trains its own data set

Yolov3 trains its own data set

Caikeng Alibaba cloud Kex_ exchange_ identification: read: Connection reset by peer

做完数据治理,质量依旧很差

Which is a good noise reduction Bluetooth headset? Ranking of the most cost-effective noise reduction Bluetooth headsets

faster-rcnn 训练自己的数据集

Knowledge points of MySQL (12)
随机推荐
Some understanding of the rank sum of matrix and the rank of image
Yolov3 trains its own data set
How to prevent XSS in PHP
Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
[LeetCode]巧用位运算
Traverse, delete, judge whether JSON data is empty, and recursion
普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
简单使用 MySQL 索引
Due to lack of funds, Changdian technology sold some assets of Xingke Jinpeng for 120million US dollars!
Huawei Kirin 985 mass production in the third quarter: TSMC 7Nm EUV process, integrated 5g baseband!
[SWT] scrolling container to realize commodity list style
There are more than 20 categories of the four operators in MySQL. It took three days and three nights to sort them out. Don't collect them quickly
Complete guide on how to prevent cross site scripting (XSS) attacks
Intel plans to sell baseband chip business, but Apple has given up?
Qt设计机器人仿真控制器——按键控制机器人关节转动
LaneATT
2.19 haas506 2.0 development tutorial - Bluetooth - Bluetooth communication (only supports versions above 2.2)
Image label processing -- converting JSON files into PNG files
【LeetCode】Day102-螺旋矩阵 II
【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)