1 条题解
-
0
一道非常简单的模拟题,通过读题你会得知你需要的是找如何能够让 + 和 - 进行中和之后剩下的值是最小的,但是只要稍微思考就会发现,我们若想让浮动最小,就需要让 + 和 - 在一个段内,显然的,只有所有 + 和 - 都在一块它们才能够互相抵消,得到的值才是最小的。
所以题目就转变成为求 + 和 - 各自数目差值的绝对值。
#include<bits/stdc++.h> #define int long long #define pii pair<int,int> #define endl '\n' using namespace std; const int mod = 1e9 + 7, inf = 1e18, N = 2e5 + 5; void solve() { int n; cin >> n; string s; cin >> s; int ans = 0, num1 = 0, num2 = 0; for (int i = 0; i < n; i++) { if (s[i] == '-') { num1++; } else { num2++; } } ans = abs(num1 - num2); cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr),cout.tie(nullptr); int T = 1; cin >> T; while(T--) { solve(); } return 0; }
- 1
信息
- ID
- 1049
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- 递交数
- 85
- 已通过
- 24
- 上传者