#186-[栈]法力水晶

版权声明:反正也没有人会转,下一个 https://blog.csdn.net/drtlstf/article/details/85011343

Description

tigertang有 个法力水晶,第i个法力水晶有固定的法力值Wi。然而如果相邻的法力水晶法力值之和为奇数,那么它们之间就会发生法力碰撞,以至于两两消除。现在已知每个法力水晶的法力值,tigertang想知道在所有的法力碰撞发生之后,剩下的水晶数量是多少。

Input

第一行一个正整数n。第二行n个整数,代表W1,W2,W3,.....Wn 。

Output

一行,一个非负整数,代表剩下的水晶数量。

7
1 1 2 3 4 4 7
  • Sample Input

1
  • Sample Output

HINT

对60%的数据,1<=n<=1000 。
对100%的数据, 1<=n<=1000000.

用一个栈辅助

#include <iostream>
#include <cstdio>
#include <stack>

using namespace std;

stack<int> s;

int main(void)
{
	int n, x;
	
	scanf("%d", &n);
	while (n--)
	{
		scanf("%d", &x);
		x &= 1; // 模2
		if (s.empty()) // 空的,防止RE,直接入栈
		{
			s.push(x);
		}
		else if (s.top() ^ x) // 奇偶性不同,加起来是奇数,这个不要入栈,栈顶弹出
		{
			s.pop();
		}
	 	else // 消除不了,入栈
		{
			s.push(x);
		}
	}
	
	printf("%d", s.size());
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/drtlstf/article/details/85011343
今日推荐