【思维构造】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;
}
相关推荐
D_evil__6 小时前
【Effective Modern C++】第二章 auto:6. 当auto推导的类型不符合要求时,使用显式类型初始化习惯用法
c++
哈哈不让取名字6 小时前
基于C++的爬虫框架
开发语言·c++·算法
剑锋所指,所向披靡!9 小时前
C++之类模版
java·jvm·c++
C+-C资深大佬10 小时前
C++风格的命名转换
开发语言·c++
No0d1es10 小时前
2025年粤港澳青少年信息学创新大赛 C++小学组复赛真题
开发语言·c++
点云SLAM10 小时前
C++内存泄漏检测之手动记录法(Manual Memory Tracking)
开发语言·c++·策略模式·内存泄漏检测·c++实战·new / delete
好评12410 小时前
【C++】二叉搜索树(BST):从原理到实现
数据结构·c++·二叉树·二叉搜索树
zylyehuo10 小时前
error: no matching function for call to ‘ros::NodeHandle::param(const char [11], std::string&, const char [34])’
c++·ros1
星火开发设计11 小时前
C++ 函数定义与调用:程序模块化的第一步
java·开发语言·c++·学习·函数·知识
天赐学c语言11 小时前
1.20 - x的平方根 && vector的扩容机制以及删除元素是否会释放内存
c++·算法·leecode