HDU-2273 The buses

来源:互联网 发布:香港代购mac口红多少钱 编辑:程序博客网 时间:2024/05/01 00:14

The buses

Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 659    Accepted Submission(s): 173
Problem Description
Garfield applied for a good job recently, and he will go to work soon by car or bus. Garfield is very broody, sometimes when he sits on the bus to wait for the traffic light, he thinks about how long all the buses pass the traffic turning. 
  Now we describe the situations when the buses stop at the traffic turning to wait for the traffic light. First the light is red, then when the light changes to green, all the buses are prepared to move. And at the beginning, all the buses are close to each other without any space, and they have different lengths and the largest speeds. We assume any car can reach the speed that isn’t beyond the maximal speed at once.
  Now Garfield wants you to calculate minimal time all the buses pass the turning.

 
Input
There are many cases. For each case, there is two intergers N(1<=N<=100), representing the number of the buses. There are two interges in the following N lines, for the length Li(meter, 1<=Li<=10) and the maximal speed Si(meter/second, 1<=Si<=10) of the i-th bus.
 
Output
For each case, print the result obtaining two digits after the decimal point.
 
SampleInput
21 22 3
 
SampleOutput
1.50

水题:

第一次以组队的形式参加组队赛,

感谢盛巨巨提供的代码。

思路如下:

此题关键点,决定整体时间的是速度最小的那辆车。

若速度最小的车排在最后,所经过的路程最长(多出全部的车长),时间最长。

所以最小的车应放在第一位,时间即总车长/最小速度;

还是用了结构体来存储数据(原谅我提的想法),其实还可以更加简单点的。


代码如下

#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;struct Node{int len;int v;}bus[101];bool cmp(Node a,Node b){    return a.v<b.v;}int main(){    int n,i;    double s;    while(cin>>n)    {        s=0;        for(i=0;i<n;i++)        {            cin>>bus[i].len;            cin>>bus[i].v;        }        sort(bus,bus+n,cmp);        for(i=0;i<n;i++)        {            s+=bus[i].len;        }        printf("%.2lf\n",s/bus[0].v);    }    return 0;}



0 0
原创粉丝点击