poj 3262 牛毁坏花问题 贪心算法
2024-09-29 11:00:43
题意:有n头牛,每头牛回去都需要一定时间,如果呆在原地就会毁坏花朵。问:怎么安排使得毁坏的花朵最少?
思路:
拉走成本最高的。
- 什么是成本?毁坏花朵的数量。
- 例如有两种排序 (这里用(a,b)表示题意,a表示往回走的时间,b表示呆在原处毁坏花朵的数量
(a,b) (c,d) 成本为 x*b+(x+a)*d;
(c,d) (a,b) 成本为 x*d+(x+c)*b;
两种成本显示,如果ad>bc则选择下面那种排序,如果bc>ad则选择上面那种排序,实现的代码
bool cmp(Cow a, Cow b)
{
return a.d / a.t > b.d / b.t;
}
通过这种成本由高到低的排序,只要遍历,时间*毁坏的花朵累加既可以得到答案
解决问题的代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Cow
{
double t, d;
}cow[];
bool cmp(Cow a, Cow b)
{
return a.d / a.t > b.d / b.t;
}
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf%lf", &cow[i].t, &cow[i].d);
sort(cow, cow + n, cmp);
long long sum = , ans = ;
for (int i = ; i < n; i++)
{
sum += ans * * cow[i].d;
ans += cow[i].t;
}
printf("%d\n", sum);
return ;
}
最新文章
- Codeforces CF#628 Education 8 F. Bear and Fair Set
- 论SOA架构的几种主要开发方式
- Harp – 内置常用预处理器的静态 Web 服务器
- hdu 1255
- Pyqt QSystemTrayIcon 实现托盘效果
- 基于Spark1.3.0的Spark sql三个核心部分
- tensorflow1
- iOS AFNetworking “Request failed: unacceptable content-type: text/html”问题
- linux服务之drbd
- mongodb csv 文件导入数据库,删除特定字段
- JHipster的安装
- ABAP ALV DEMO示例源码
- java-常用快捷键
- 线性代数(高斯消元):JSOI2008 球形空间产生器sphere
- Spring之SpringMVC的Controller(源码)分析
- linux之x86裁剪移植---ffmpeg的H264解码显示(420、422)
- python接口自动化(十九)--Json 数据处理---实战(详解)
- python基础16_闭包_装饰器
- servlet对象的生命周期
- js一元运算符
热门文章
- (转)nginx域名访问的白名单配置梳理
- 找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args)
- 安卓H5软键盘遮挡输入框
- AJPFX辨析continue与break的区别
- Java面向对象(static、final、匿名对象、内部类、包、修饰符、代码块)
- Linux下软件安装的四种方式
- 用jQuery实现jsonp跨域
- 关于IT公司招聘的一个思考
- Android RecyclerView使用GridLayoutManager间距设置
- Beginning Python Chapter 2 Notes