当前位置:网站首页>Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array 题解
Educational Codeforces Round 100 (Rated for Div. 2) B. Find The Array 题解
2022-07-21 15:05:00 【Is_TuTouYa】
前排感谢 @Leonard.7 大佬给的思路qwq
没有大佬的帮助我这个菜批A不掉这道题qwq
原题
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,其数组各个数的和为S,让你求一个数组b符合以下条件:
- b[i] ∈ [1,1e9]
- 2*∑|a[i]-b[i]| <= S
- b[i]可以被b[i+1]整除或被整除
这里是一个基本上压着最大值的解法。
以数组1,3,2,5,4举例,首先把这个数组以单双数分开,即
1 ,2, 4 和 3,5
被分开的这两个数组的其中一个的和必然大于S/2。
在这个例子中也就是1,2,4。
这样看来如果我们想要求出数组b,即数组b中的数为1,x,2,x,4。
显而易见的是x=1。此题得证。
以下是写的非常不简便的代码
#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]; //求S
if(j % 2==0) b[j] = 1;
else b[j] = a[j];
sum+=abs(a[j]-b[j]); //计算∑|a[i]-b[i]|
}
if(2*sum < tot) //两种情况进行判断,这里又又双写麻烦了
for(int j = 0;j < x;j++)cout << b[j] << " ";
else{
//第二种情况
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;
}
边栏推荐
- AT&T伪指令 以及对于CFI CFA的解释
- js实现一个Promises/A+ 规范的Promise
- Jupyter使用TIPS
- [MySQL and database] MySQL & database Chapter 10: function learning
- ECCV 2022 oral "unicorn" unifies the network structure and learning paradigm of four target tracking tasks for the first time, and SOTA on eight challenging data sets
- ps小白從0開始……
- String... 可变长度参数列表
- Mysql基础命令
- C language - how to use structs and struct pointers?
- EF数据迁移
猜你喜欢
随机推荐
application.yml 和 application.propertise 配置不生效
Idea change font size
oracle rac一个节点重启后公网网卡丢失,Device eth3 does not seem to be present, delaying initialization
Arcgis图层标注显示
vi编辑器常用命令
Keithley software 2600 series 2611b | 2612b | 2614b | 2634b ns SourceMeter source table software
C#字体使用效果
输入一个URL到这个页面呈现出来,这个过程发生了什么?
Ride switch theme
FileNotFoundError: [Errno 2] No such file or directory: ‘cmake‘
[MySQL and database] MySQL & database Chapter 9: learning stored procedures
开发动态 | StoneDB 2022年版本发布里程碑
微信小程序列表渲染之上拉加载更多
稀土开发者大会|Apache Pulsar Committer 刘德志分享云原生技术变革之路
What does it mean that the profit and loss of financial products are negative?
win10下载地址
第三版新视野大学英语读写教程4结业考点(1,2,3,5,6单元)
模板方法模式
Foundation of Mathematics: Jensen inequality
ArcGIS创建矢量