Description
Input
Output
一个数代表他到达终点所需要的最少时间,精确到小数点后两位数
Sample Input
2 2
10 20
2 4
3 2
1 5 2
2 3
Sample Output
10.00
3.67
分析:最简单的贪心
AC代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e3+10;
int a[maxn];
int b[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int m,n;
double sum=0;
while(cin>>m>>n)
{
sum=0;
for(int i=0; i<m; i++) cin>>a[i];
for(int i=0; i<n; i++) cin>>b[i];
sort(a,a+m);
sort(b,b+n);
int i,j;
for(i=m-1,j=n-1; i>=0 && j>=0; i--,j--)
{
sum+=((double)a[i]/b[j]);
}
while(i>=0)
{
sum+=a[i];
i--;
}
printf("%.2lf\n",sum);
}
return 0;
}

本文介绍了一个简单的贪心算法应用案例——通过合理分配加速器来最小化跑跑卡丁车游戏中到达终点所需的时间。输入包括路程数量、加速器数量及各自的属性,输出则是经过优化后的最短时间。
3123

被折叠的 条评论
为什么被折叠?



