E. Mishap in Club

题目:

样例1:

输入
+-+-+

输出
1

 样例2:

输入
---

输出
3

思路:

        数学贪心模拟思路,由于不知道在俱乐部的人数和在外面的人数,又要尽可能少的人数,那么定义两个变量,一个是里面的人数 in = 0,外面的人数 out = 0,然后根据记录变化,尽可能的少,进去的,可以是 外面进去的,出来的,可以是在里面的出来的,然后里面和外面的人数总的相加,就是尽可能少的人数。

代码详解如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) (x).begin(),(x).end()
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;

inline void solve()
{
	string s;
	cin >> s;
	
	int in = 0,out = 0;
	
	for(auto i : s)
	{
		if(i == '+')
		{
			// 进入俱乐部的人数累加
			++in;
			// 如果外面有确定的人数,那么是确定的外面人数进来
			if(out) --out;	
		}else
		{
			// 出去的人数累加
			++out;
			// 如果里面有确定的人数,那么是确定的里面人数出去
			if(in) --in;
		}
	}
	
	// 总确定人数累加
	int ans = in + out;

	cout << ans << endl;
}

signed main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
//	cin >> _t;
	while (_t--)
	{
		solve();
	}

	return 0;
}

最后提交:

猜你喜欢

转载自blog.csdn.net/hacker_51/article/details/133500080
今日推荐