POJ 3262 Protecting the Flowers
来源:互联网 发布:pink cat动作数据 编辑:程序博客网 时间:2024/05/31 15:18
题意:
输入N头牛分别离开的时间T和每份时间吃花的数量D,牵一头牛回去,那其他牛则吃花的数量为各自吃花数量的总和乘以当前牵的牛离开的时间,然后再乘以2,因为,一去一回。问怎么让牛吃的花的数量最少,求出最少?
思路:
用到了贪心的方法,其实可以用D/T的值来排序,从大到小,因为值越大,代表吃花相对越多,则先牵,这里面涉及到一些规律,自己可以举一两例子揣摩一下。
代码:
#include<stdio.h>#include<algorithm>using namespace std;struct Node{ int T,D;}x[100010];int cmp(Node a,Node b){ return (a.D*1.000/a.T)>(b.D*1.000/b.T);}int main(){ int N; scanf("%d",&N); for(int i=0;i<N;i++) scanf("%d %d",&x[i].T,&x[i].D); sort(x,x+N,cmp);//排序 long long sum=0,sumT=0; for(int i=0;i<N;i++) { sum+=sumT*x[i].D;//当前牵的这头牛所吃花的数量再加上前面牛吃花的数量 sumT+=x[i].T*2;//时间累加,为下头牛吃花的时间 } printf("%lld\n",sum); return 0;}
0 0
- POJ 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ-3262-Protecting the Flowers
- Poj 3262 Protecting the flowers
- POJ 3262 Protecting the Flowers
- POJ-3262-Protecting the Flowers
- POJ-3262-Protecting the Flowers
- poj-3262-Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ 3262 - Protecting the Flowers
- POJ 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- poj 3262 Protecting the Flowers
- POJ 3262 Protecting the Flowers
- hibernate 增删改查
- 前端开发面试题集锦(二)
- centos6.5禁用ipv6
- 1、CSS3新增的选择器
- android学习经验
- POJ 3262 Protecting the Flowers
- 操作系统的基础知识
- 启用\禁用windows无线网卡探索初步2--问题解决
- 2、CSS3中字体与文本的相关属性
- c++计算两矩阵欧氏距离
- Swift编程语言学习1.1——常量与变量
- 3、背景、边框和边框的相关属性
- Graphics pipeline
- 德州怎样治疗失眠