Zhejiang university----To Fill or Not to Fill
2016-03-29 16:21
211 查看
//Zhejiang university----To Fill or Not to Fill
/*
时间限制 1s 内存限制 128M
题目描述:
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 <algorithm>
#include <math.h>
using namespace std;
struct station{
double pi;//该加油站单位油的价格
int di;//杭州到此加油站的距离
bool operator < (const station &b) const{
return di < b.di;
}
}buf[501];
int main(){
int cmax;//邮箱容量
int d;//从杭州到目的地的距离
int davg;//每单位油可以行驶的距离
int n;//加油站个数
int i;
while(scanf("%d%d%d%d",&cmax,&d,&davg,&n) != EOF){
double sum = 0;
int k = 0;
for(i=0;i<n;i++){
scanf("%lf%d",&buf[i].pi,&buf[i].di);
}
buf
.di = d;
buf
.pi = 1000000.0;
int muxd = cmax * davg;//加满油可以行驶的距离
sort(buf,buf+n);
double remained_gas = 0;//剩余油量
for(i=0;i<n;i++){
if(buf[0].di != 0){
printf("The maximum travel distance = 0.00\n");
break;
}else{
k = i+1;
if(i != 0)
remained_gas -= ((double)(buf[i].di-buf[i-1].di))/davg;
for(;k<n&&buf[k].pi>=buf[i].pi;k++)
continue;//直到k>=n || buf[k].pi<buf[i].pi时才停止k++
if(buf[k].di - buf[i].di > muxd){
sum += (cmax - remained_gas)*buf[i].pi;
remained_gas = cmax;
}else{
double tmp;
tmp = ((double)(buf[k].di-buf[i].di))/davg - remained_gas;
if(tmp > 0){
//fabs(tmp)>1e-5,1e-5 = 10^(-5) = 0.00001
sum += tmp * buf[i].pi;
remained_gas = ((double)(buf[k].di - buf[i].di))/davg;
}
}
if(buf[i+1].di - buf[i].di > muxd){
printf("The maximum travel distance = %.2lf\n",(double)(buf[i].di+muxd));
break;
}
}
}
if(i==n) printf("%.2lf\n",sum);
}
return 0;
}
/*
时间限制 1s 内存限制 128M
题目描述:
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 <algorithm>
#include <math.h>
using namespace std;
struct station{
double pi;//该加油站单位油的价格
int di;//杭州到此加油站的距离
bool operator < (const station &b) const{
return di < b.di;
}
}buf[501];
int main(){
int cmax;//邮箱容量
int d;//从杭州到目的地的距离
int davg;//每单位油可以行驶的距离
int n;//加油站个数
int i;
while(scanf("%d%d%d%d",&cmax,&d,&davg,&n) != EOF){
double sum = 0;
int k = 0;
for(i=0;i<n;i++){
scanf("%lf%d",&buf[i].pi,&buf[i].di);
}
buf
.di = d;
buf
.pi = 1000000.0;
int muxd = cmax * davg;//加满油可以行驶的距离
sort(buf,buf+n);
double remained_gas = 0;//剩余油量
for(i=0;i<n;i++){
if(buf[0].di != 0){
printf("The maximum travel distance = 0.00\n");
break;
}else{
k = i+1;
if(i != 0)
remained_gas -= ((double)(buf[i].di-buf[i-1].di))/davg;
for(;k<n&&buf[k].pi>=buf[i].pi;k++)
continue;//直到k>=n || buf[k].pi<buf[i].pi时才停止k++
if(buf[k].di - buf[i].di > muxd){
sum += (cmax - remained_gas)*buf[i].pi;
remained_gas = cmax;
}else{
double tmp;
tmp = ((double)(buf[k].di-buf[i].di))/davg - remained_gas;
if(tmp > 0){
//fabs(tmp)>1e-5,1e-5 = 10^(-5) = 0.00001
sum += tmp * buf[i].pi;
remained_gas = ((double)(buf[k].di - buf[i].di))/davg;
}
}
if(buf[i+1].di - buf[i].di > muxd){
printf("The maximum travel distance = %.2lf\n",(double)(buf[i].di+muxd));
break;
}
}
}
if(i==n) printf("%.2lf\n",sum);
}
return 0;
}
相关文章推荐
- LeetCode16. 3Sum Closest
- linux中共享库(动态链接库)的延迟载入
- Excel的数据类型转换方法
- Ajax异步上传文件
- 快速提升Mac运行速度方法—CleanMyMac轻松搞定
- 51Nod 算法马拉松12 移数博弈
- 数据库处理千万级数据的解决方案
- Edon 和 survivor 占比配置项 -XX:SurvivorRatio=8
- Uva11300(数学)
- CocoaPods详解之----制作篇
- js获取某个div下的input
- 算法学习之一,排序算法
- Android进阶-IPC机制
- map和object互相转换
- Linux U盘分区格式化 fdisk命令
- OpenGL名词解释笔记
- 执行索引的前提条件
- ASP.NET使用Quartz.NET实现定时任务调度
- 如何选择Mac清理软件
- 代码模版