3 条题解
-
1
贪心
假设当前位于第i个加油站,后面有比i费用低的加油站j:
- j在行驶范围内,那就加油量刚好到j就可以,去j再加油。
- j不在行驶范围内,那就加满油,去后面寻找比它费用低的
code
#include <bits/stdc++.h> using namespace std; #define ll long long namespace syr { const ll N = 5e4+10; struct node { ll d; //距离 ll p; //价格 }a[N]; ll n, c, s, L, ans; ll l[N]; deque <ll> q; bool cmp (node a, node b) { return a.d<b.d; } void work() { cin>>n>>c>>s>>L; for (ll i=1; i<=n; i++) cin>>a[i].d>>a[i].p; a[n+1] = {L, 1000000000}; n++; sort(a+1, a+1+n, cmp); for (ll i=1; i<n; i++) { if (a[i+1].d-a[i].d>c) { cout<<-1<<'\n'; return; } } for (ll i=n; i>=1; i--) { while (!q.empty() && a[q.front()].d-c>a[i].d) q.pop_front(); while (!q.empty() && a[q.back()].p>a[i].p) q.pop_back(); if (!q.empty()) l[i] = q.back(); else l[i] = i; q.push_back(i); } if (s<a[1].d) { cout<<-1<<'\n'; return; } s -= a[1].d; for (ll i=1; i<=n; ) { if (i==l[i]) { if (a[n].d-a[i].d<=c) { ll t = max((ll)0, a[n].d-a[i].d-s); cout<<ans+t*a[i].p<<'\n'; return; } ans += (c-s)*a[i].p; s = c-(a[i+1].d-a[i].d); i++; }else { ll dis = a[l[i]].d-a[i].d; if (s>=dis) { s -= dis; i = l[i]; continue; } ans += (dis-s)*a[i].p; s = 0; i = l[i]; } } } } int main() { cin.tie(0)->sync_with_stdio(0); syr::work(); return 0; }完结散花~~
- 1
信息
- ID
- 74
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- (无)
- 递交数
- 44
- 已通过
- 10
- 上传者