fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long computeCompressionLoss(vector<int> featureMap) {
  5. long long ans = 0;
  6. long long run = 0;
  7.  
  8. for (int x : featureMap) {
  9. if (x == 1) {
  10. run++;
  11. } else {
  12. ans += run * (run + 1) / 2;
  13. run = 0;
  14. }
  15. }
  16.  
  17. // last run (if array ends with 1s)
  18. ans += run * (run + 1) / 2;
  19.  
  20. return ans;
  21. }
  22.  
  23. int main() {
  24. // your code goes here
  25. int n;
  26. cin >> n;
  27. vector<int> v(n);
  28. for (int i=0;i<n;i++) cin >> v[i];
  29. cout << computeCompressionLoss(v) << endl;
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
1 1 1 0 0
stdout
6