Codeforces 466 A. Cheap Travel
2014-09-13 10:21
344 查看
A. Cheap Travel
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket
for m rides (she can buy it several times). It costs b rubles.
Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?
Input
The single line contains four space-separated integers n, m, a, b (1 ≤ n, m, a, b ≤ 1000)
— the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride
ticket.
Output
Print a single integer — the minimum sum in rubles that Ann will need to spend.
Sample test(s)
input
output
input
output
Note
In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy threem ride tickets.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
long long int n,m,a,b;
cin>>n>>m>>a>>b;
long long int k=n/m;
long long int ans=n*a;
for(int i=0;i*m<2*n;i++)
{
int r=n-m*i;
if(r<0) r=0;
ans=min(ans,r*a+i*b);
}
if(n%m==0)
ans=min(ans,k*b);
else
ans=min(ans,(k+1)*b);
cout<<ans<<endl;
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket
for m rides (she can buy it several times). It costs b rubles.
Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?
Input
The single line contains four space-separated integers n, m, a, b (1 ≤ n, m, a, b ≤ 1000)
— the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride
ticket.
Output
Print a single integer — the minimum sum in rubles that Ann will need to spend.
Sample test(s)
input
6 2 1 2
output
6
input
5 2 2 3
output
8
Note
In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy threem ride tickets.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
long long int n,m,a,b;
cin>>n>>m>>a>>b;
long long int k=n/m;
long long int ans=n*a;
for(int i=0;i*m<2*n;i++)
{
int r=n-m*i;
if(r<0) r=0;
ans=min(ans,r*a+i*b);
}
if(n%m==0)
ans=min(ans,k*b);
else
ans=min(ans,(k+1)*b);
cout<<ans<<endl;
return 0;
}
相关文章推荐
- Codeforces 466 E. Information Graph
- Codeforces 466 D. Increase Sequence
- codeforces 466-C. Number of Ways(前缀和+尺取)
- codeforces 466 C Number of Ways
- Codeforces 466 E. Information Graph
- CodeForces 466 D.Increase Sequence(组合数学+dp)
- 【CODEFORCES】 A. Cheap Travel
- Codeforces 466 C. Number of Ways
- Codeforces 466 B. Wonder Room
- CodeForces 225C. Barcode
- CodeForces 631A 631A - Interview
- CodeForces 682A Alyona and Numbers
- codeforces 717 G. Underfail(费用流,好题)
- 【CodeForces - 808C Tea Party】 贪心
- CodeForces 198B Jumping on Walls(bfs || dfs)
- Codeforces 437D The Child and Zoo(贪心+并查集)
- codeforces 631B B. Print Check
- Codeforces B. Escape
- 【CodeForces】500B - New Year Permutation(Floyd)(贪心)
- Codeforces 106 D. Treasure Island(前缀和预处理)