ZeroJudge D660: Jumping Mario

UVa 11764 – Jumping Mario

Mario is at the final castle. He needs to jump over some walls to enter Koopa's room and defeat the monster to save the princess. For this problem, we only focus on the 'jumping over walls' part. You will be given the heights of N walls (from left to right). Mario is currently standing at the first wall. He must jump to adjacent walls until he reaches the last one. This means he will jump N – 1 times. 'A high jump' means Mario jumps to a taller wall, and 'a low jump' means Mario jumps to a shorter wall. Can you find the total number of high jumps and low jumps?

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input is an integer T (T < 30), indicating the number of test cases to follow. Each test case starts with a positive integer N (N < 50), representing the number of walls. The next line contains N integers representing the heights of the walls (from left to right). Each height is a non-negative integer not exceeding 10.For each test case, first output the test case number. Then, output two numbers representing the total number of 'high jumps' and 'low jumps'.
3
8
1 4 2 2 3 5 3 4
1
9
5
1 2 3 4 5
Case 1: 4 2
Case 2: 0 0
Case 3: 4 0

Thought Process

Before collecting data, declare a variable 'current' representing the current height of the wall. When collecting data, first check if the received number is the first number in the sequence. If it is not, then determine if the received number is higher or lower than 'current'. Heights of the same value are not counted. Make sure to update 'current' with the received number at the end.

Sample Code-ZeroJudge D660: Jumping Mario

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    for (int i = 0; i<T; i++) {
        int N, current = 0, high = 0, low = 0;
        cin >> N;
        for (int j = 0; j<N; j++) {
            int tmp;
            cin >> tmp;
            if (j != 0) {
                if (tmp > current) high++;
                else if (tmp < current) low++;
            }
            current = tmp;
        }
        cout << "Case " << i+1 << ": " << high << " " << low << "\n";
    }
}

//ZeroJudge D660
//Dr. SeanXD

Comments