ZeroJudge F818: Natural Selection

"Survival of the fittest" is the brutal law of competition in nature. Take lions, for example; each lion wants to stand out from the pride and become the lion king. After each lion cub is born, the lioness selectively nurtures the strong ones. The lioness invests her energy and food in these cubs with innate advantages because they have a better chance of becoming future lion kings. As for the weaker cubs, they not only face neglect from their mother and rejection from their peers but also risk being abandoned altogether.

To protect the weaker lion cubs, people want to preemptively bring back the cubs that would be abandoned by the lioness and conserve them in the city. They observe each lion cub's weight (in kilograms) multiplied by its height (in centimeters) to compare them and select the one with the minimum value to bring back for protection. Can you help write a program to accomplish this lion cub conservation effort?

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line of input is an integer N (2 ≤ N ≤ 2000), representing the total number of lion cubs. The second line contains N integers representing the height Hi (1 ≤ Hi ≤ 1000, 1 ≤ i ≤ N) of each lion cub. The third line contains N integers representing the weight Wi (1 ≤ Wi ≤ 1000, 1 ≤ i ≤ N) of each lion cub.輸出一行為要帶走的小獅子的身高與體重,兩數中間以空白格隔開。測資
保證只會有一隻小獅子有最小的體重-身高乘積).
2
145 34
21 55
34 55
4
361 619 555 855
419 758 549 240
361 419
5
17 100 40 111 68
157 151 25 173 197
40 25

Thought Process

You can use an array/vector to first record the values of height. Then, while collecting the weight data, perform the multiplication directly and check for the minimum value. You can start by setting the weight of the first lion cub as the initial value for the minimum variable.

Sample Code-ZeroJudge F818: Natural Selection

#include <iostream>
#include <vector>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    vector<long long int>height;
    for (int i = 0; i<N; i++)
    {
        long long int tmp;
        cin >> tmp;
        height.push_back(tmp);
    }
    long long int min, ans_h, ans_w;
    for (int i = 0; i<N; i++)
    {
        long long int weight;
        cin >> weight;
        long long int calc = weight * height[i];
        if (i == 0)
        {
            min = weight * height[i];
            ans_h = height[i];
            ans_w = weight;
        }
        if (calc < min)
        {
            min = calc;
            ans_h = height[i];
            ans_w = weight;
        }
    }
    cout << ans_h << " " << ans_w << endl;
}

//ZeroJudge F818
//Dr. SeanXD

Comments