ZOJ2256 Mincost
2017-04-16 19:36
330 查看
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
题目的意思是某市出租车计价按一下标准: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 long
const 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;
}
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
题目的意思是某市出租车计价按一下标准: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 long
const 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;
}
相关文章推荐
- ZOJ 2256 Mincost
- ZOJ 2256 Mincost
- zoj 2256 Mincost
- zoj 2256 Mincost
- ZOJ-2256
- ZOJ 3471 状态压缩DP
- zoj1082
- ZOJ_1008_GnomeTetravex
- ZOJ 1085 Alien Security(SPFA+dfs)
- ZOJ 1136 / POJ 1465 Multiple
- Friends zoj 3710
- zoj 1562 Oil Deposits (DFS)
- ZOJ 3712Hard to Play(水题)
- ZOJ Problem Set - 3202 Second-price Auction
- ZOJ 3635 Cinema in Akiba (第一次组队) 树状数组+二分
- ZOJ_1204_Additive equations
- ZOJ1654 Place the Robots
- ZOJ-1101-Gamblers
- zoj 3203 Light Bulb(三分法)
- ZOJ 1967 POJ 2570 Fiber Network