[Codewars] Web Polygons

Web Polygons

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<vector>
using namespace std;
using ull = unsigned long long;
ull webpolygons(const std::vector<int>& s)
{
ull res = 0, mul = 1;
vector<pair<ull,ull>> st{{1,1}};
for(auto& x : s) {
if(x > 1) st.push_back({1,x}), mul *= x;
else if(st.size()) {
auto [a,b] = st.back(); st.pop_back();
mul /= b;
st.back().first *= a * b;
res += b * (b - 1) / 2 * a * a * mul;
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/31/PS/Codewars/web-polygons/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.