//codeup To Fill or Not to Fill //汽油容量 距离 每个gas可跑距离 汽油站数量 //59 525 19 2 //单价 距离 //3 314 //3 0 #include<iostream> #include<algorithm> usingnamespace std; typedefstructstation { double price; double start; double end; }; boolcmp(station a, station b){ return a.start < b.start; } intmain(){ int max, meter, per_meter, n; station sta[510]; while (cin >> max >> meter >> per_meter >> n) { for (int i = 0; i < n; i++) { cin >> sta[i].price >> sta[i].start; sta[i].end = sta[i].start + max * per_meter; }
sort(sta, sta + n, cmp); /*for (int i = 0; i < n; i++) { cout << "排序结果\n"; cout << sta[i].start << " " << sta[i].end << " "<<sta[i].price<<endl; }*/ if (sta[0].start != 0 || n == 0) { cout << "The maximum travel distance = "; printf("%.2f\n",0.00); break; } for (int i = 0; i < n; i++) { if (sta[i].end > meter) { sta[i].end = meter; } for (int j = i + 1; j < n; j++) { if (sta[j].start < sta[i].end) { if (sta[j].price <= sta[i].price) {//新的更便宜 if(sta[i].start>sta[j].start &&sta[i].end<sta[j].end){//被包围 sta[i].end = sta[i].start; break; } sta[i].end = sta[j].start; } else { sta[j].start = sta[i].end; } } else {//没有加油站了 break; } } } double money = 0, all_meter = 0; for (int i = 0; i < n; i++) { //cout << sta[i].start << " " << sta[i].end << endl; all_meter += sta[i].end - sta[i].start; money += (sta[i].end - sta[i].start) / per_meter * sta[i].price; } if (all_meter < meter) { cout << "The maximum travel distance = "; printf("%.2f\n", all_meter); } else { printf("%.2f\n", money); } } }