1033 To Fill or Not to Fill (25分)(有空重寫一次

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1000;

struct Oil
{
	double prize, dis;
}a[maxn];

bool cmp(Oil a, Oil b) {
	 return a.dis < b.dis;
}

int main() {
	double c, d, davg; int n;
	scanf("%lf%lf%lf%d", &c, &d, &davg, &n);
	double odis = c * davg;
	for (int i = 0; i < n; i++)
	{
		scanf("%lf%lf", &a[i].prize, &a[i].dis);
	}
	a[n].prize = 0; a[n].dis = d;
	sort(a, a + n, cmp);
	/*for (int i = 1; i <= n; i++)
	{
		printf("%f %f\n", a[i].prize, a[i].dis);
	}*/

	if (a[0].dis!=0)
	{
		printf("The maximum travel distance = 0.00\n");

	}
	else {
		int t = 0;
		double tank = 0, cost = 0;
		while (t < n)
		{
			int k = -1;
			double prizeMin = 1000000000;
			for (int i = t + 1; i <= n && a[i].dis - a[t].dis <= odis; i++)
			{
				if (prizeMin > a[i].prize)
				{
					prizeMin = a[i].prize;
					k = i;
					if (prizeMin < a[t].prize)
					{
						break;
					}
				}
			}
			if (k == -1)
			{
				break;
			}
			double need = (a[k].dis - a[t].dis) / davg;
			if (prizeMin < a[t].prize)
			{
				if (tank < need)
				{
					cost += (need - tank)*a[t].prize;
					tank = 0;
				}
				else
				{
					tank -= need;
				}
			}
			else {
				cost += (c - tank)*a[t].prize;
				tank = c - need;
			}
			t = k;
		}if (t == n)
		{
			printf("%.2f\n", cost);
		}
		else
		{
			printf("The maximum travel distance = %.2f\n", a[t].dis + odis);
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章