【思维构造】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;
}
相关推荐
WW_千谷山4_sch6 小时前
洛谷B3688:[语言月赛202212]旋转排列(新解法:deque双端队列)
数据结构·c++·算法
漂流瓶jz6 小时前
UVA-11214 守卫棋盘 题解答案代码 算法竞赛入门经典第二版
c++·算法·dfs·aoapc·算法竞赛入门经典·迭代加深搜索·八皇后
fpcc7 小时前
并行编程实战——CUDA编程的Enhancing Memory Allocation
c++·cuda
白太岁7 小时前
通信:(3) 高并发网络通信:epoll + 边沿触发 + 非阻塞 IO + tcp
c语言·网络·c++·网络协议·tcp/ip
楼田莉子8 小时前
C++项目:日志&&线程池
linux·c++·学习·visual studio code
tankeven8 小时前
HJ93 数组分组
c++·算法
汉克老师9 小时前
GESP2024年3月认证C++二级( 第一部分选择题(9-15))
c++·循环结构·分支结构·gesp二级·gesp2级
plus4s10 小时前
2月19日(85-87题)
c++·算法
白太岁11 小时前
Redis:(2) hiredis 使用、C++ 封装与连接池
c语言·c++·redis·缓存
Desirediscipline11 小时前
cerr << 是C++中用于输出错误信息的标准用法
java·前端·c++·算法