Array problem

So, the problem is the following:

There is an array of buildings, with various heights, in a position. You have to find the ones that have view to the sea, because you want to see how many options for hotel reservations there are.

Sample Input

5
20 45 25 30 15


Sample Output

2

Only 2 have view to the sea because they are taller than other previous hotels.

So I thought that I would make a boolean that is false, except if there aren't any taller buildings before the i position in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;


int main() {
    int ans=0;
    bool view=false;
    int n;
    cin>>n;
    int hotels[n];
    for (int z=0; z<n; z++){
        cin>>hotels[z];
    }
    for (int i=1; i<n; i++){
        for (int j=0; j<i-1; j++){
            if (hotels[i]>hotels[j])
            view=true;
            else
            view=false;
        }
        if (view==true)
            view=false;
            ans++;
    }
    cout<<ans+1; //+1 because the 1st will always have view.
    return 0;
}


But in some test cases it just doesn't work.
Last edited on
closed account (48T7M4Gy)
Which end, right or left is the sea? By the sound of it the 15 units high one is closest to the sea.
Ah, I forgot to mention it. Hotels[0] is the one closest to the sea. I actually made a mistake on the example. Let me edit it.
Last edited on
closed account (48T7M4Gy)
Hmm so wouldn't that mean only hotel[0] and hotel[1] can see the sea, ie 2 hotels not 3 ?

If you go from the other side you get 3 hotels, namely 4,3 and 1.
Last edited on
Yeah. Just corrected it.
Last edited on
closed account (48T7M4Gy)
Anyway, whichever way it is another algorithm is:

1. take the rightmost hotel height and subtract it from all the others on the left
2. then take the rightmost positive height and subtract that from the others on the left
3. keep repeating until none left

any with a remaining height of zero are 'the ones'.
Why does this work?
closed account (48T7M4Gy)
Try it. You don't have to write the program, just pencil and paper will do. And you can check whether I'm right. I must admit the comment starting with ' any with a ... ' is not exactly right, but the essence is there without being misleading.

Once you convince yourself you can then make an 'informed decision' whether you proceed to code it up or not. It shouldn't take more than about 5 minutes withe five hotels. 1 minute more likely. :)

Thanks for the help :). I will do it now and try to understand.
closed account (48T7M4Gy)
 
 20   45   25  30 15
 5   25   10  15   0    <-- (-15)
-10  10   - 5    0         <-- (-15)
-20    0                      <-- (-10)
   
 
 20   45   25    30    15
 0     25   5      10    -5    <-- (-20)
 0     0     -20   -15  -30   <-- (-25)

FTFY
Last edited on
closed account (48T7M4Gy)
FTFY

I can't see why because going from the left hand end you only get 2 hotels as you show and I mentioned earlier which is why I queried it with the OP before, against the sample output.
So UYA
Last edited on
Hotels[0] is the one closest to the sea.

Sample Input

5
20 45 25 30 15

Sample Output

2


That's why.
closed account (48T7M4Gy)
Like I said UYA, you're behaving like a girl.
I'm sorry if you can't handle somebody pointing out a mistake you've made. I wasn't trying to be confrontational, just helping OP get the best info.
closed account (48T7M4Gy)
Obviously you can't follow a thread or comprehend simple english. I'm sorry to mistake you for a girl. You're more like a grub. MYOFB.
I followed the thread perfectly well. I fixed your output, which was working off of the original output, to work off of the edited output, which OP changed after his third post, yet you kept going off of anyway. Please stop with the insults. This place should be for safe discussion.
closed account (48T7M4Gy)
Stop arguing the point grub. You didn't follow the thread and tried to be smart with your sarcastic FTFY. If you want to use that sort of tone then expect a return on the same volume. You're the one making this place unsafe and unpleasant with your smart arse street talk and lack of education. Learn to read english!
Topic archived. No new replies allowed.