博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
奶牛问题
阅读量:5013 次
发布时间:2019-06-12

本文共 2558 字,大约阅读时间需要 8 分钟。

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input
Line 1: A single integer
N
Lines 2..
N+1: Each line contains two space-separated integers,
Ti and
Di, that describe a single cow's characteristics
Output
Line 1: A single integer that is the minimum number of destroyed flowers
Sample Input
63 12 52 33 24 11 6
Sample Output
86
Hint
FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.
 
 
#include
#include
using namespace std;struct node{int t,d;};bool cmp(node a,node b){ return a.d*b.t>a.t*b.d;}node a[100];int main(){ int n; cin>>n; int i; for(i=0;i
>a[i].t>>a[i].d; } sort(a,a+n,cmp); i=1; int sum=0; for(i=0;i

 

 

 

 

 

#include
#include
typedef long long ll;using namespace std;struct node{int t,d;};bool cmp(node a,node b){ return a.d*b.t>a.t*b.d;}node a[100010];int main(){ int n; ll sum, num; while(cin>>n){ int i; sum = 0; for(i=0;i
>a[i].t>>a[i].d; sum+=a[i].d; } sort(a,a+n,cmp); num = 0; for(i=0;i

 

转载于:https://www.cnblogs.com/zsy831143/p/9016900.html

你可能感兴趣的文章
oracle服务起不来以及无法监听问题解决
查看>>
Mvc--Html.ActionLink()的用法
查看>>
delphi 基础书籍推荐
查看>>
《面向对象程序设计》2018年春学期寒假及博客作业总结
查看>>
iOS开发UI之KVC(取值/赋值) - KVO (观察某个对象的某个属性的改变)
查看>>
1.7 将一个MxN矩阵所有为0的元素所在行和列全部置0
查看>>
删除U盘时提示无法停止‘通用卷’设备的解决方法!!不要每次都硬拔了,对电脑有不小的损害!!!...
查看>>
Java中接口与接口和类之间的关系
查看>>
芯片TPS70925
查看>>
linux shell 发送email 附件
查看>>
人群密度估计 CrowdCount
查看>>
JSON.parse()和JSON.stringify()
查看>>
.net 常用正则表达式
查看>>
Java泛型中的标记符含义:
查看>>
初遇GitHub
查看>>
[C# 网络编程系列]专题八:P2P编程
查看>>
Jsの练习-数组常用方法 -forEach()
查看>>
动态绑定treeview的方法
查看>>
jvm参数
查看>>
3-1 案例环境初始化
查看>>