九度OJ 题目1437:To Fill or Not to Fill
来源:互联网 发布:opencv python puttext 编辑:程序博客网 时间:2024/06/06 05:51
一题目描述:
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
输入:
For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.
输出:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
样例输入:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
二.题目分析
贪心加模拟
三.代码
#include <stdio.h>#include <stdlib.h>#define MAXN 501#define INF 999999 typedef struct Sta{ double dist; double perprice;}Station;int cmp(const void*a,const void*b){ return (*(Station*)a).dist-(*(Station*)b).dist;}int main(){ double Cmax,D,Davg,sumprice,maxdist,curgas; int N,i,j; Station S[MAXN]; while(scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N)!=EOF) { for(i=0;i<N;i++) scanf("%lf%lf",&S[i].perprice,&S[i].dist); S[N].dist=D; S[N].perprice=INF; qsort(S,N+1,sizeof(S[0]),cmp); if(S[0].dist>0) { printf("The maximum travel distance = 0.00\n"); continue; } //init sumprice=0; maxdist=Cmax*Davg; curgas=0; for(i=0;i<N;i++) { //for station i,first comupte current gas,then compute if need add gas,update current gas if(i!=0) curgas -= (S[i].dist-S[i-1].dist)/Davg; if((S[i+1].dist-S[i].dist)>maxdist) { printf("The maximum travel distance = %.2f\n",S[i].dist+Cmax*Davg); break; } //for i ,there are three continues //find minner price j for(j=i+1;j<N&&S[j].perprice>=S[i].perprice;j++) continue; //to judje if j is arriveded // arrived if((S[j].dist-S[i].dist)<maxdist) { //if gas is not enough if(curgas*Davg<(S[j].dist-S[i].dist)) { sumprice += ((S[j].dist-S[i].dist)/Davg-curgas)*S[i].perprice; curgas = (S[j].dist-S[i].dist)/Davg; } //else gas is enough,go on } // canot find j ,then add full gas at i else { sumprice += (Cmax-curgas)*S[i].perprice; curgas = Cmax; } } if(i==N) printf("%.2f\n",sumprice); } return 0;}
- 九度oj 1437 To Fill or Not to Fill
- 【学习笔记】〖九度OJ〗题目1437:To Fill or Not to Fill
- 九度OJ 题目1437:To Fill or Not to Fill
- 九度OJ 题目1437:To Fill or Not to Fill
- 九度OJ-浙大机试-题目1437:To Fill or Not to Fill
- To Fill or Not to Fill (九度贪心题目)
- 九度OJ #1437 To Fill or Not to Fil
- 九度OJ 1437 To Fill or Not to Fill -- 贪心算法
- 九度OJ 1437 To Fill or Not to Fill ( 贪心算法)
- 九度OJ 1437 To Fill or Not to Fill -- 贪心算法
- 题目1437:To Fill or Not to Fill 题目25
- 25-题目1437:To Fill or Not to Fill
- 题目1437:To Fill or Not to Fill
- 题目1437:To Fill or Not to Fill
- To Fill or Not to Fill PAT/九度OJ 分析
- 九度考研真题 浙大 2012-3浙大1437:To Fill or Not to Fill
- 题目25:To Fill or Not to Fill
- 一道贪心题目(To Fill or Not to Fill )
- Git新手向引导——Git进阶
- Axis2开发WebService客户端 的3种方式
- 知识点五:BluetoothAdapter类分析
- 诸葛亮《诫子书》
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
- 九度OJ 题目1437:To Fill or Not to Fill
- NOIP2009最优贸易 (tarjan+dfs)
- Magento 把所有产品的Special Price的值,给Group Price,然后清空Special Price的值
- Linux下进程通信方式
- AsyncTask与Handler
- java简介
- leetcode 日经贴,Cpp code -Repeated DNA Sequences
- Jquery常用工具函数总结
- hdu 3338 网络流填数字