Analysis of past CSP-J semi-final exam questions | 2023 T2 Highway

Learn C++ from a young age! Record the questions in the CSP-J exam preparation study process and record every moment.

Attached is a summary post: Analysis of past CSP-J semi-final questions | Summary_Blog of a communicator who loves programming-CSDN Blog


[Title description]

Xiaobao is going to drive along the highway.

There are a total of n stations on the highway, numbered from 1 to n. The distance between site i and site i+1 is vi kilometers.

You can refuel at every station on the highway. The price of one liter of oil at the station numbered i is ai yuan, and each station only sells an integer liter of oil.

Xiaobao wants to drive from site 1 to site n. At first, Xiaobao is at site 1 and the car's fuel tank is empty. It is known that the car's fuel tank is large enough to hold any amount of oil, and each liter of oil can allow the car to travel d kilometers. Ask Xiaobao, how much does it cost to drive from station 1 to station n? How much does it cost to refuel?

【Input format】

The first line of input contains two positive integers n and d, which respectively represent the number of stops on the road and the distance the car can travel per liter of oil.

The second line of input contains n-1 positive integers v1, v2...vn-1, which respectively represent the distance between sites.

The second line of input contains n positive integers a1, a2...an, which respectively represent the prices of refueling at different stations.

[Output format]

Output a line containing only one positive integer, indicating the minimum amount of gas Xiaobao needs to spend to drive from site 1 to site n.

【Input sample】

5 4 
10 10 10 10
9 8 9 6 5

【Output sample】

79

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int n, d, k=0, minn=1e9;
long long cost = 0;
int a[100005] = {0};
int v[100005] = {0};
int main()
{
    cin >> n >> d;
    for (int i=1; i<n; i++) {
        cin >> v[i];
    }
    for (int i=1; i<=n; i++) {
        cin >> a[i];
    }
    for (int i=1; i<n; i++) {
        minn = min(minn, a[i]);
        if (k>=v[i]) {
            k = k - v[i];
            continue;
        }
        cost += 1ll * ceil(1.0 *(v[i]-k)/d) * minn;
        k = 1ll * ceil(1.0 *(v[i]-k)/d)*d - (v[i]-k);
        // cout << "cost k " << cost << " " << k << endl;
    }
    cout << cost << endl;
    return 0;
}

【operation result】

5 4
10 10 10 10
9 8 9 6 5
79

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133990171