HDU3420 Bus Fair
2015-07-20 10:26
417 查看
Bus Fair
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
You are now in Foolish Land. Once moving in Foolish Land you found that there is a strange Bus fair system. The fair of moving one kilometer by bus in that country is one coin. If you want to go to X km and your friend wants to go
to Y km then you can buy a ticket of X+Y coins (you are also allowed to buy two or more tickets for you two).
Now as a programmer, you want to show your creativity in buying tickets! Suppose, your friend wants to go 1 km and you want to go 2 km. Then it’s enough for you to buy a 2coin ticket! Because both of you are valid passengers before crossing the first km. and
when your bus cross the first km your friend gets down from the bus. So you have the ticket of 2km! And you can safely reach to your destination, 2km using that ticket.
Now, you have a large group of friends and they want to reach to different distance. You think that you are smart enough that you can buy tickets that should manage all to reach their destination spending the minimum amount of coins. Then tell us how much we
should at least pay to reach our destination.
Input
There are multiple test cases. Each case start with a integer n, the total number of people in that group. 0<=n<=1000. Then comes n integers, each of them stands for a distance one of the men of the group wants to go to. You can assume
that the distance a man wants to go is always less than 10000.
Output
Your program should print a single integer for a single case, the minimum amount of coins the group should spend to reach to the destination of all the members of that group.
Sample Input
2
1
2
2
2
3
Sample Output
2
4
题意
n 个人一起坐车,下了车的人可以把票给其他的人,只要满足当前的票数够所有人到下一站,就不用买票,否则就需要补足不够的票。问最少满足所有人都到达目的地的票数是多少。
分析
判断 n 人到达当前站的 cost 是否够 n-1 个人到达下一站,不够则更新 cost 为 n-1 人到达下一站的总花费,否则继续判断 cost 是否能让 n-2 个人再次到站,直到判断到所有人都到站为止。状态转移方程为 cost = (n-i) * a[i]。
AC代码如下
#include <cstdio>
#include <algorithm>
#define maxn 1000+5
using namespace std;
int a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 0 ; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int cost = 0;
for(int i = 0 ; i < n; i++)
if(cost < (n-i) * a[i])
cost = (n-i) * a[i];
printf("%d\n",cost);
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Description
You are now in Foolish Land. Once moving in Foolish Land you found that there is a strange Bus fair system. The fair of moving one kilometer by bus in that country is one coin. If you want to go to X km and your friend wants to go
to Y km then you can buy a ticket of X+Y coins (you are also allowed to buy two or more tickets for you two).
Now as a programmer, you want to show your creativity in buying tickets! Suppose, your friend wants to go 1 km and you want to go 2 km. Then it’s enough for you to buy a 2coin ticket! Because both of you are valid passengers before crossing the first km. and
when your bus cross the first km your friend gets down from the bus. So you have the ticket of 2km! And you can safely reach to your destination, 2km using that ticket.
Now, you have a large group of friends and they want to reach to different distance. You think that you are smart enough that you can buy tickets that should manage all to reach their destination spending the minimum amount of coins. Then tell us how much we
should at least pay to reach our destination.
Input
There are multiple test cases. Each case start with a integer n, the total number of people in that group. 0<=n<=1000. Then comes n integers, each of them stands for a distance one of the men of the group wants to go to. You can assume
that the distance a man wants to go is always less than 10000.
Output
Your program should print a single integer for a single case, the minimum amount of coins the group should spend to reach to the destination of all the members of that group.
Sample Input
2
1
2
2
2
3
Sample Output
2
4
题意
n 个人一起坐车,下了车的人可以把票给其他的人,只要满足当前的票数够所有人到下一站,就不用买票,否则就需要补足不够的票。问最少满足所有人都到达目的地的票数是多少。
分析
判断 n 人到达当前站的 cost 是否够 n-1 个人到达下一站,不够则更新 cost 为 n-1 人到达下一站的总花费,否则继续判断 cost 是否能让 n-2 个人再次到站,直到判断到所有人都到站为止。状态转移方程为 cost = (n-i) * a[i]。
AC代码如下
#include <cstdio>
#include <algorithm>
#define maxn 1000+5
using namespace std;
int a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i = 0 ; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
int cost = 0;
for(int i = 0 ; i < n; i++)
if(cost < (n-i) * a[i])
cost = (n-i) * a[i];
printf("%d\n",cost);
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解