1033. To Fill or Not to Fill (25)

来源:互联网 发布:dos攻击域名 编辑:程序博客网 时间:2024/05/28 05:16
#include #include #include #include #define inf 9999using namespace std;struct binary{double price, oil;binary(double p, double o) :price(p), oil(o) {};bool operator<(const binary &b)const { return oil < b.oil; }};struct Oil{binary* jy;Oil(double p, double o) :jy(new binary(p,o)) {};bool operator<(const Oil &b)const { return jy->price < b.jy->price; }};struct Nowoil{list oils;float sum() {float res = 0;for (auto it : oils) res += it.jy->oil;return res;}void useoil(float i) {oils.sort();while (i!=0){if (oils.front().jy->oil >= i) {oils.front().jy->oil -= i;i = 0;}else {i -= oils.front().jy->oil;oils.front().jy->oil = 0;}if (oils.front().jy->oil == 0) oils.pop_front();}}};double Cmax, D, Davg, N;list station;int main(){cin >> Cmax >> D >> Davg >> N;D /= Davg;for (int i = 0; i < N; i++) {double price, dis;cin >> price >> dis;dis /= Davg;station.push_back(binary(price, dis));}station.push_back(binary(0, D));station.sort();double need = 0;double now = 0;double nowprice=0;Nowoil nowoil;while (true) {if (nowoil.sum() < station.front().oil - now) {printf("The maximum travel distance = %.2f", (now + nowoil.sum())*Davg);break;}else {nowoil.useoil(station.front().oil - now);now = station.front().oil;for (auto it : nowoil.oils) {if (station.front().price < it.jy->price) {nowprice -= (it.jy->price - station.front().price)*it.jy->oil;it.jy->price = station.front().price;}}if (nowoil.sum() < Cmax) {nowprice += (Cmax - nowoil.sum())*station.front().price;nowoil.oils.push_back(Oil(station.front().price, Cmax - nowoil.sum()));}}station.pop_front();if (station.empty()) {printf("%.2f",nowprice);break;}}return 0;}
原创粉丝点击