#CodeForces 1749B (Death's Blessing)

10 messages · Page 1 of 1 (latest)

fallen slate
#

I got the concept correct, but I keep getting runtime errors. I'm not very experienced with why different runtime errors happen. Does anyone know why I'm getting a runtime error?

dreamy spireBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

fallen slate
#

!format

dreamy spireBOT
#
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// find the index where the largest value of b[i] is stored (largest spell)
// denoted as vi_max (vector-index max)
int max_vector_index(vector<int> const& v, int& vi_max) {
  for (int i = 0; i < v.size(); ++i) {
    vi_max = max(v[i], v[i + 1]);
  }
  return vi_max;
}

void solve() {
  int n, vi_max, h = 0;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);

  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    h += a[i];
  }

  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    h += b[i];
  }
  max_vector_index(b, vi_max);

  h -= b[vi_max];
  cout << h << '\n';
}

int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
techy
lunar jolt
#

you should say what its' supposed to do, and also post the error you see

#

vi_max = max(v[i], v[i + 1]); this doesn't do what you want it to do

warm robin
#

Your max_vector_index reads memory out of bounds where i + 1 equals v.size() which would explain runtime errors.

#

also, I don't think it returns the index but rather the largest value which causes another OOB read in solve

#

you could use std::max_element to get the index for the max element like this

template <class FIt>
typename std::iterator_traits<FIt>::difference_type
max_element_index(
    FIt first, 
    FIt last
    ) 
{
    return std::distance(
               first, 
               std::max_element(first,
                                last));
}
dreamy spireBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.