【Educational Codeforces Round 74 (Rated for Div. 2) C】Standard Free2play

题目链接

Standard Free2play

Introduce

You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height (h), and there is a moving platform on each height (x) from (1) to (h).

Each platform is either hidden inside the cliff or moved out. At first, there are (n) moved out platforms on heights (p_1,p_2,…,p_n). The platform on height (h) is moved out (and the character is initially standing there).
If you character is standing on some moved out platform on height (x), then he can pull a special lever, which switches the state of two platforms: on height (x) and (x−1). In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another.

Your character is quite fragile, so it can safely fall from the height no more than (2). In other words falling from the platform (x) to platform (x−2) is okay, but falling from (x) to (x−3) (or lower) is certain death.

Sometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height (h), which is unaffected by the crystals). After being used, the crystal disappears.

What is the minimum number of magic crystal you need to buy to safely land on the (0) ground level?

Input

The first line contains one integer (q) ((1≤q≤100)) — the number of queries. Each query contains two lines and is independent of all other queries.

The first line of each query contains two integers (h) and (n) ((1≤h≤10^9, 1≤n≤min(h,2⋅10^5))) — the height of the cliff and the number of moved out platforms.

The second line contains (n) integers (p_1,p_2,…,p_n) ((h=p_1>p_2>⋯>p_n≥1)) — the corresponding moved out platforms in the descending order of their heights.

The sum of n over all queries does not exceed (2⋅10^5).

Output

For each query print one integer — the minimum number of magic crystals you have to spend to safely come down on the ground level (with height (0)).

Example

input

4
3 2
3 1
8 6
8 7 6 5 3 2
9 6
9 8 5 4 3 1
1 1
1

output

0
1
2
0

题解

题意是你站在高度为(h)的悬崖上,每个整数高度都有一个台阶(刚开始的时候还以为只有给出的高度有台阶o(╥﹏╥)o)。
给你(n)个高度,表示这些高度的台阶是突出来的,其他高度的台阶都是没有凸出来的(只有凸出来的台阶才能踩)。
你可以进行一个操作,让你所在的高度台阶缩进去,并且改变你下一个台阶的状态(让凸出来的缩进去,缩进去的凸出来)。
此时你会下落到下一个突出来的台阶,你下落的高度不能大于(2),你可以消耗一次水晶(the crystal)改变一个台阶的状态。
求最少消耗多少水晶可以到达地面(高度为(0))。

因为你要把当前台阶缩进去的时候,只会改变(h-1)高度的状态,所以可以发现,如果凸出的台阶不相连,那么这些台阶是不会影响的。
接下来我们考虑一长串相邻的台阶凸出来的时候,你会发现缩回当前台阶的时候,下落的高度刚好为(2)
因为每次下落(2)个台阶,所以我们只要考虑相邻台阶数量的奇偶性就行了。
我们发现当脚下台阶相邻个数为奇数时,不需要水晶,偶数时,需要一个水晶使脚下台阶变成奇数。
所以我们只要寻找有多少相邻的台阶数量为奇数就行了。
这里要注意:当你在最顶上时,直接判断奇偶就行了,但是在后面的时候,判断时要把台阶数(+1),因为你往下跳的时候,你脚下也是有一个台阶的,当你脚下的台阶和凸出的台阶相连的时候,这一段相连的台阶数就会(+1)

接下来我们再考虑一下两端相连的台阶中间只有(1)阶台阶缩进去的情况。
如果上面那一段的台阶数是偶数((+1)之前),那么上面那一段对下面那一段的答案没有影响。
如果上面那一段的台阶数是奇数((+1)之前),如果我们把最后一阶凸出的台阶缩进来,那么对答案没有影响。
如果我们让两段之间的台阶凸出来,分类讨论一下:

  • 下面那段台阶数是奇数((+1)之前),分开算答案为(2),合并之后台阶数为奇数((+1)之前),合并时消耗(1),合并后还要消耗(1),总共消耗(2),答案不变。
  • 下面那段台阶数是偶数((+1)之前),分开算答案为(1),合并之后台阶数为偶数((+1)之前),合并时消耗(1),合并后无需消耗,总消耗为(1),答案不变。
    所以最后我们找到相邻凸出台阶数为奇数(第一段台阶数为偶数)的数量就是答案了。
    上代码:
#include<bits/stdc++.h>
using namespace std;
int q,h,n;
int p[200009];
int ans,ss;
bool k;
int main(){
	scanf("%d",&q);
	while(q--){
		ans=0;ss=1;k=0;
		scanf("%d%d",&h,&n);
		for(int j=1;j<=n;j++)
			scanf("%d",&p[j]);
		p[n+1]=0;
		for(int j=2;j<=n+1;j++){
			if(p[j]!=p[j-1]-1){
				if(ss%2==0) ans++;
				ss=2;
			}else ss++;//用ss来记录到当前位置连续数量
		}
		printf("%d
",ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/linjiale/p/11656820.html