0

im a new problem solver. recently i came across this problem on codeforces website. i managed to get the two values required for each turn depending on the number of turn given by the user, but i cant find the highest number of passenger at stop out of all.

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int turn,get_off,get_on,total_passenger,highest_total;
    cin>>turn;
    int* stops=new int(turn);
    for(int i=0;i<turn;i++){
        cin>>get_off>>get_on;
        total_passenger=get_on-get_off;
        stops[i]=total_passenger;
    }
    for(int i=0;i<turn;i++){
        if(stops[i]>stops[i+1]){
            highest_total=stops[i];
        }
    }
    cout<<highest_total<<endl;
    return 0;
}
Maisha
  • 45
  • 1
  • 1
  • 8
  • `int* stops=new int(turn);` Allocates a single `int`. Did you mean `int* stops=new int[turn];`? – Algirdas Preidžius May 29 '20 at 11:00
  • stackoverflow.com is not a web site for "problem solvers" but for ***specific*** questions on programming topics. All questions here must have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. This question must be [edit]ed, and all links removed and replaced with all relevant information, as plain text. All code must meet all requirements of a [mre]. You can find many other questions here that explain everything in plain text. There's no reason why this one can't, either. – Sam Varshavchik May 29 '20 at 11:02
  • why are you doing stops[i] with an Int pointer? – ΦXocę 웃 Пepeúpa ツ May 29 '20 at 11:03
  • yess, but after i corrected it, the answer is still wrong. i think my logic is wrong here and i cant find out what it is – Maisha May 29 '20 at 11:04
  • [Why using namespace std; is considered bad practise](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – darclander May 29 '20 at 11:04

4 Answers4

1

you can try using https://en.cppreference.com/w/cpp/algorithm/max_element also you set the total passengers number to the difference between get off and get on and I think you should just add that difference, and to allocate memory for int* you use [] instead of ()

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int turn,get_off,get_on,total_passenger,highest_total;
    cin>>turn;
    int* stops=new int[turn];
    for(int i=0;i<turn;i++){
        cin>>get_off>>get_on;
        total_passenger+=get_on-get_off;
        stops[i]=total_passenger;
    }
    cout<<std::max_element(stops, stops + turn)<<endl;
}
SzymonO
  • 432
  • 3
  • 15
1

The issue seems to be with int *stops = new int(turn). It will allocate memory for 1 integer. Use int stops[turn] and your algorithm need to be modified. stop[i] += total_passenger need to be used with some if-else.

darclander
  • 1,526
  • 1
  • 13
  • 35
rng70
  • 25
  • 9
0

Firstly: int* stops=new int(turn); initializes a pointer to a new int with the value turn. See int a = 0 and int a(0) differences

Instead you should either use int* stops=new int[turn];, read about when you should use new here: When should I use the new keyword in C++?. An important note is that you have to delete the variable in order to avoid a Memory leak.

Or (which I would say is the case you should use in this scenario), int stops[turn];. Besides that I can not argue with your logic since I believe this is not a forum to help you solve code, although the syntax seems correct.

I would recommend reading Why is "using namespace std;" considered bad practice? if you are interested in learning C++ though.

darclander
  • 1,526
  • 1
  • 13
  • 35
0

You can initiate stops as an array I guess(int stops[turn]) as C++ now offers to allocate memory this way even inside any function. That may solve your problem.

Alongside you will have to check corner cases as your highest_total variable may provide garbage value. So while declaring the variable assign a value to it. I prefer initializing int highest_total=-1 here.

Actually you dont need an array or pointer here. This can be solved this way-

#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int turn,get_off,get_on,total_passenger=0;
    cin>>turn;
    int highest_total=-1;
    for(int i=0;i<turn;i++){
        cin>>get_off>>get_on;
        total_passenger=total_passenger+get_on-get_off;
        if(total_passenger>highest_total)
             highest_total=total_passenger;
    }
    cout<<highest_total<<endl;
    return 0;
}

Raju Ahmed
  • 366
  • 6
  • 18
  • i tried it exactly like this i remember, but it gives wrong answer. in this code, we only get the difference of each turn, but we should be also adding the previous value to new turn, this makes me so cinfused – Maisha May 29 '20 at 11:28
  • this is why i used pointer array to store them and then compare, otherwise id have to declare 4 variable for them – Maisha May 29 '20 at 11:30
  • I have edited my code and hope now you get accepted. Basically just initialized total_passenger variable to '0' and each time inside for loop added the value with the previous **total_passenger** value. – Raju Ahmed May 29 '20 at 11:33
  • Ahh. Feels good to help. Actually it can also be done with the pointers and arrays. But some of my teachers advised me not to use pointers where it is not actually necessary as it creates problem with your programs efficiency. – Raju Ahmed May 29 '20 at 11:58
  • @Maisha as Raju says this can be done with pointers aswell, although I would not claim this reduces the efficiency but instead you might have to deal with memory allocation. I provided some links in my answer who might help you get started. – darclander May 29 '20 at 12:18
  • @RajuAhmed im so inclined to that idea because it came to my mind first when thinking of how to solve this problem. if im not being overly annoying and of course if you arent busy and dont mind to help, can you give me some ideas why my code is failing? i still wanna fix my original code since there is no reason why it shouldnt work – Maisha May 29 '20 at 15:19
  • @darclander yes, i will look into it now, thank you :) – Maisha May 29 '20 at 15:20
  • @Maisha if any of these answers helped you / made you resolve your issues you could upvote them and mark one answer who answered your question to help people who might get the same problem in the future! – darclander May 29 '20 at 16:52