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.

题意

  • 给一个长度为\(n\)的数组,通过多次选择两个相邻且奇偶性相同的数变为他们的乘积,得到相邻数奇偶性都不同的数组,问最小操作数

思路

  • 奇 × 奇 = 奇
  • 偶 × 偶 = 偶
  • 只需要检查相邻数奇偶性相同的有多少对即可

代码

点击查看代码
#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();
}

陌生词

  1. even numbers【偶数】
  2. odd numbers【奇数】
  3. in other words【换句话说】
  4. modulo【取模】
  5. the given operation【给定的操作】
  6. constant【恒定的】
  7. invariant【不变量】
  8. Implementation【实现】
posted @ 2023-01-22 10:47  Keith-  阅读(52)  评论(0编辑  收藏  举报