Official

A - Find Takahashi Editorial by en_translator


In order to find the highest bridge, inspect the bridges one by one from the first one, and record the highest bridge you have seen so far.
When examining a new bridge, compare the highest bridge you have come across with the new one, and remember the higher one.
After scanning all the bridges, the highest one is the answer.
This process can be written with for and if statements.

Another solution is to compare each bridge with the others. It does not matter in case of this problem, but the time complexity is worse than the original one, so we recommend you to learn the original one.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int h[101];
  int ans;

  cin >> n;
  for(int i=1;i<=n;i++)cin>>h[i];

  ans=1;
  for(int i=2;i<=n;i++){
    if(h[ans]<h[i])ans=i;
  }

  cout << ans <<endl;
  return 0;
}

Sample code in Python:

n=int(input())
h=list(map(int, input().split()))

ans=1
for i in range(2,len(h)+1):
    if(h[ans-1]<h[i-1]):
      ans=i

print(ans)

posted:
last update: