#include <cstddef>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
vector<vector<ll>> parseVectorFromString(const string& s) {
vector<vector<ll>> res;
res.push_back(vector<ll>());
stringstream ss(s);
string token;
while (getline(ss, token, ',')) {
// size_t start = token.find_first_not_of(" ");
// size_t end = token.find_last_not_of(" ");
// if (start != string::npos && end != string::npos) {
// token = token.substr(start, end - start + 1);
// }
// if (!token.empty()) {
ll val = stoi(token);
// 分段
if (val <= 0) res.push_back(vector<ll>());
else res.back().push_back(val);
// }
}
return res;
}
int main() {
string s;
getline(cin, s);
auto paras = parseVectorFromString(s);
ll ans = 0;
for (auto para : paras) {
// 注意要判空,否则会有段错误,空的段不用处理
if (para.empty()) continue;
vector<ll> need(para.size(), 1);
for (int i = 1; i <= para.size() - 1; i++)
if (para[i] > para[i - 1]) need[i] = max(need[i], need[i - 1] + 1);
for (int j = para.size() - 2; j >= 0; j--)
if (para[j] > para[j + 1]) need[j] = max(need[j], need[j + 1] + 1);
// for (int i = 0; i < para.size(); i++)
// cout << para[i] << " " << need[i] << endl;
// cout << endl;
for (int x : need) ans += x;
}
cout << ans;
}
// 64 位输出请用 printf("%lld")
先将逗号分隔格式的输入解析到vector里,解析同时可以完成基于无效请求的分段。然后,对于每个段,求需要分配的token数,从前往后、从后往前各遍历一遍,注意要用 max(need[i], need[i - 1] + 1) 和 max(need[j], need[j + 1] + 1),防止被覆盖。

京公网安备 11010502036488号