Files
python/TangDou/CSP-J/MKX_B/3241.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

55 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100010;
int v[N]; // 站点间的距离
int a[N]; // 每个站点的油价
int n; // n个站点
double d; // 1升油可以跑多少公里
int money; // 一共花费
int r; // 油箱的油可以跑的公里数
/*
5 4
10 10 10 10
9 8 9 6 5
答案:79
*/
int getMoney(int begin, int end) { // 左闭右开
int length = 0;
for (int i = begin; i < end; i++) length += v[i];
if (r >= length) {
// 不用加油
r -= length;
return 0;
} else {
// 需要加油, 计算加多少油: 油箱的油还有剩余,还能跑几公里
int add = ceil((length - r) / d);
// 计算加油费用
int x = a[begin] * add;
// 跑到 end, 油箱的油还能跑几公里
r = add * d + r - length;
return x;
}
}
signed main() {
freopen("road.in","r", stdin);
freopen("road.out", "w", stdout);
cin >> n >> d; // n个站点,每升油可以让车前进d公里
for (int i = 1; i < n; i++) cin >> v[i]; // 站点间的距离 a1~a2,a2~a3,...
for (int i = 1; i <= n; i++) cin >> a[i]; // 不同站点加油的价格
int i = 1;
while (i < n) {
int j = i + 1;
// 找到下一个便宜的加油站
while (j <= n && a[j] >= a[i]) j++;
money += getMoney(i, j);
i = j;
}
cout << money << endl;
}