A. Everybody Likes Good Arrays!【Codeforces Round #845 (Div. 2) 】
A. Everybody Likes Good Arrays!
原题链接
An array a is good if for all pairs of adjacent elements【相邻元素】, ai and ai+1 (1≤i<n) are of different parity【奇偶性】. Note that an array of size 1 is trivially【非常】 good.
You are given an array of size n.
In one operation you can select any pair of adjacent elements in which both elements are of the same parity, delete them, and insert their product in the same position.
Find the minimum number of operations to form a good array.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤100).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
Output
For each test case print an integer, the minimum number of operations required to form a good array.
Example
input
3
5
1 7 11 2 13
4
1 2 3 4
6
1 1 1 2 2 3
output
2
0
3
Note
Consider the first test case. Select the 2-nd and the 3-rd integers and apply the operation on them. The array changes from [1,7,11,2,13] to [1,77,2,13]. Next, select the 1-st and the 2-nd integers, array changes from [1,77,2,13] to [77,2,13]. Thus we require 2 operations. It can be proved that this is the minimum number of operations.
In the second test case, the given array is already good. So we require 0 operations.
题意
- 给一个长度为
的数组,通过多次选择两个相邻且奇偶性相同的数变为他们的乘积,得到相邻数奇偶性都不同的数组,问最小操作数
思路
- 奇 × 奇 = 奇
- 偶 × 偶 = 偶
- 只需要检查相邻数奇偶性相同的有多少对即可
代码
点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
#define X first
#define Y second
typedef long long LL;
const char nl = '\n';
const int N = 110;
int n,m;
int a[N];
void solve(){
m = 0;
cin >> n;
for(int i = 1; i <= n; i ++)cin >> a[i];
for(int i = 1; i < n; i ++){
if(a[i] % 2 == a[i+1]%2)m++;
}
cout << m << nl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;
cin >> T;
while(T --){
solve();
}
//solve();
}
陌生词
- even numbers【偶数】
- odd numbers【奇数】
- in other words【换句话说】
- modulo【取模】
- the given operation【给定的操作】
- constant【恒定的】
- invariant【不变量】
- Implementation【实现】
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】