ZOJ2256-Mincost

来源:互联网 发布:淘宝买家秀的福利网站 编辑:程序博客网 时间:2024/06/06 01:27

Mincost

Time Limit: 2 Seconds      Memory Limit: 65536 KB

The cost of taking a taxi in Hangzhou is not a constant for each kilometer you travel: the first 4 kilometers costs 10 yuan (yuan is the monetary unit in China), even if you don't finish it; the next 4 kilometers costs 2 yuan each and the price for the rest of the trip is 2.4 yuan per kilometer; the last part of the trip is regarded as 1 kilometer even if it's shorter. A traveller may reduce the cost by reseting the meter at the middle of the trip if used wisely. For example, if the trip is 16 kilometers, he should cut it into two parts with the same length, each half will cost 18 yuan, with an overall cost of 36, less than the original price of 37.2. Now, given the length of the trip, find the minimum cost.


Input

The input contains several cases, each has one positive integer in a seperate line, representing the length of the trip. All input data are less than 10000000. Proceed until a case with zero, which should be ignored.


Output

For each case, output the minimum cost, leave one digit after decimal point if NECESSARY.


Sample Input

3
9
16
0


Sample Output

10
20.4
36



Author: PAN, Minghao
Source: ZOJ Monthly, November 2004


题思:某市出租车计价按一下标准:4公里以内受10元,4~8每公里收2元,8公里以外每公里2.4元。现给出一段距离,中途可以下车,问最少要花多少钱?

解题思路:可以发现坐8公里平均价格最小,所以我们把距离尽可能分成8公里,多出来的部分算一下按8公里以外计价和重新乘哪个便宜取哪个


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int main(){    int n;    while(~scanf("%d",&n)&&n)    {        if(n<4) printf("10\n");        else if(n<=8) printf("%d\n",10+2*(n-4));        else        {            int cnt=n/8;            int x=n%8;            if(x==0) printf("%d\n",18*cnt);            else            {                double ad1=2.4*x;                double ad2=10;                if(x>4) ad2+=2*(x-4);                double ans=cnt*18+min(ad1,ad2);                if((int)ans==ans) printf("%.0f\n",ans);                else printf("%.1f\n",ans);            }       }    }    return 0;}

0 0