【思维构造】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;
}
相关推荐
同学小张1 小时前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
爱学习的小邓同学6 小时前
C++ --- 多态
开发语言·c++
招摇的一半月亮12 小时前
P2242 公路维修问题
数据结构·c++·算法
f***019313 小时前
CC++链接数据库(MySQL)超级详细指南
c语言·数据库·c++
合方圆~小文13 小时前
球型摄像机作为现代监控系统的核心设备
java·数据库·c++·人工智能
椰萝Yerosius14 小时前
[题解]2024CCPC郑州站——Z-order Curve
c++·算法
滨HI017 小时前
C++ opencv简化轮廓
开发语言·c++·opencv
学习路上_write17 小时前
FREERTOS_互斥量_创建和使用
c语言·开发语言·c++·stm32·单片机·嵌入式硬件
闻缺陷则喜何志丹18 小时前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
BestOrNothing_201519 小时前
一篇搞懂 C++ 重载:函数重载 + 运算符重载,从入门到会用(含 ++、<<、== 实战)
c++·函数重载·运算符重载·operator·前置后置++·重载与重写