c++ - Movie Weekend Codechef Beginner Section : Runtime Error -


i have written following code problem, ideone doesnt give me runtime error compiler codechef giving me one. compiler in codechef gcc 4.9.2

problem [link : https://www.codechef.com/problems/moviewkn]

little egor huge movie fan. likes watching different kinds of movies: drama movies comedy movies, teen movies horror movies. planning visit cinema weekend, he's not sure movie should watch. there n movies watch during weekend. each movie can characterized 2 integers li , ri, denoting length , rating of corresponding movie. egor wants watch 1 movie maximal value of li × ri. if there several such movies, pick 1 maximal ri among them. if there still tie, pick 1 minimal index among them. task egor pick movie watch during weekend.

expected input

the first line of input contains integer t denoting number of test cases.

the first line of test case description contains integer n(number of films).

the second line of test case description contains n integers l1, l2, ...,ln (l length of movie). following line contains n integers r1, r2, ..., rn(r rating of movie).

my input
2
2
1 2
2 1
4
2 1 4 1
2 4 1 4

expected output

for each test case, output single integer denoting index of movie. note 1-based indexing followed.

my output
1
2

explanation

in first example case, both films have same value of l × r, first film has better rating. in second example case, second , fourth movies equally good, second movie has smaller index

my solution

#include<iostream> #include<string> #include<vector> using namespace std;  int main() {      int t;      cin >> t;     cin.ignore();      if (t >= 1 && t <= 5) {          (int q = 0; q < t; q++) {               int n;             size_t pos;             cin >> n;             cin.ignore();              if (n >= 1 && n <= 100) {                  vector<int> l;                 vector<int> r;                 vector<int> c;                  (int u = 0; u < n; u++) {                      int a;                     cin >> a;                     cin.ignore();                      if (a >= 1 && <= 100) {                          l.push_back(a);                      }                   }                 (int u = 0; u < n; u++) {                      int a;                     cin >> a;                     cin.ignore();                      if (a >= 1 && <= 100) {                          r.push_back(a);                      }                   }                 (int u = 0; u < n; u++) {                      int df = (l[u] * r[u]);                     c.push_back(df);                    }                    (size_t u = 0; u < c.size(); u++) {                      int max = c[0];                       if (max < c[u]) {                          max = c[u];                         pos = u+1;                     }                      if (max == c[u]) {                          if (r[pos-1] < r[u]) { pos = u + 1; }                         if (r[pos - 1] == r[u]) {                              if (pos > u) { pos = u + 1; }                          }                      }                   }                  cout << pos << endl;               }          }      }      return 0; } 

after going through of questions on runtime error regarding codechef questions, guessing problem related accessing out of boundary element not able pinpoint actual problem

in line:

if (r[pos - 1] < r[u]) { pos = u + 1; } 

the first time around loop, pos uninitialised, leading undefined behaviour.

simple fix:

add pos = 1 before loop


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -