【思维构造】Find The Array—CF1463B

Find The Array---CF1463B

思路

我太菜了,这么简单的思路都没有想到。。。

分别计算 a a a 数组中下标为奇数的所有元素的和 sum_odd 和下标为偶数的所有元素的和 sum_even

如果 sum_odd < sum_even,将 a a a 中的所有下标为奇数的元素全部换成 1 1 1 后的 a a a 就是需要求的 b b b 数组;

如果 sum_odd > sum_even,将 a a a 中的所有下标为偶数的元素全部换成 1 1 1 后的 a a a 就是需要求的 b b b 数组;

C o d e Code Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
using PII = pair<int, int>;
using i128 = __int128;
const int N = 2e5 + 10;

int n;
int a[N];

void solve() {
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> a[i];
	
	int sum_odd = 0, sum_even = 0;
	for (int i = 1; i <= n; i ++) {
		if (i % 2) {
			sum_odd += a[i];
		} else {
			sum_even += a[i];
		}
	}
	
	for (int i = 1 + (sum_even < sum_odd); i <= n; i += 2) {
		a[i] = 1;
	}
	for (int i = 1; i <= n; i ++) {
		cout << a[i] << ' ';
	}
	cout << "\n";
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T = 1;
	cin >> T; cin.get();
	while (T --) solve();
	return 0;
}
相关推荐
蒋星熠20 小时前
全栈开发实战指南:从架构设计到部署运维
运维·c++·python·系统架构·node.js·devops·c5全栈
杜子不疼.20 小时前
【C++】深入拆解二叉搜索树:从递归与非递归双视角,彻底掌握STL容器的基石
开发语言·c++
天若有情67320 小时前
从零实现轻量级C++ Web框架:SimpleHttpServer入门指南
开发语言·前端·c++·后端·mvc·web应用
mjhcsp1 天前
C++ 三分查找:在单调与凸函数中高效定位极值的算法
开发语言·c++·算法
Elnaij1 天前
从C++开始的编程生活(13)——list和浅谈stack、queue
开发语言·c++
深思慎考1 天前
微服务即时通讯系统(服务端)——用户子服务实现逻辑全解析(4)
linux·c++·微服务·云原生·架构·通讯系统·大学生项目
草莓火锅1 天前
用c++使输入的数字各个位上数字反转得到一个新数
开发语言·c++·算法
j_xxx404_1 天前
C++ STL:阅读list源码|list类模拟|优化构造|优化const迭代器|优化迭代器模板|附源码
开发语言·c++
散峰而望1 天前
C/C++输入输出初级(一) (算法竞赛)
c语言·开发语言·c++·算法·github
曾几何时`1 天前
C++——this指针
开发语言·c++