链接:

文章目录

  • 题目描述
  • 题意:
  • 题解:
  • 代码:
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入描述:

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

输出描述:

Line 1: A single integer that is the minimum number of destroyed

flowers
示例1
输入

6
3 1
2 5
2 3
3 2
4 1
1 6

输出

86

题意:

n头牛,将第i头牛运回谷仓需要时间2*ti,在等待运输过程中每分钟吃di朵花,问怎么运输牛才能让花的损失最小?

题解:

贪心问题
有AB两头牛相邻,
A牛:Ta,Da
B牛:Tb, Db
在运A牛时,B牛在等待,等待了Ta2的时长,每分钟吃Db多花,一共吃了Ta2Db多花
同理,在运B牛时,A牛在等待,等待了Tb
2的时长,每分钟吃Da多花,一共吃了Tb2Da多花
当运输A牛比运输B牛更划算时,
Ta2Db<Tb2Da
TaDb<TbDa
然后将牛按照这个排序,这个顺序就是最佳顺序,直接计算花的损失即可

代码:

tot记录一分钟所有等待牛破坏花的数量

#include<bits/stdc++.h>
const int maxn=1e5+3;
using namespace std;
struct node{int t,d;
}a[maxn];
bool cmp(node x,node y){return x.t*y.d<x.d*y.t;
}
int main()
{int n;cin>>n;long long sum=0;long long tot=0;for(int i=1;i<=n;i++){cin>>a[i].t>>a[i].d;tot+=a[i].d;}sort(a+1,a+n+1,cmp);for(int i=1;i<=n;i++){//      cout<<"tot="<<tot<<endl;sum+=(tot-a[i].d)*a[i].t*2;tot-=a[i].d;}cout<<sum;return 0;
}

牛客网 【每日一题】5月28日题目精讲 Protecting the Flowers相关推荐

  1. 牛客网 每日一题 7月23日题目精讲—wpy的请求

    来源:牛客网: 文章目录 wpy的请求 题解: 代码: wpy的请求 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K Special Judge ...

  2. 【每日一题】8月28日题目精讲 编号

    [每日一题]8月28日题目精讲 编号 链接:https://ac.nowcoder.com/acm/problem/19925 来源:牛客网 题目描述 你需要给一批商品编号,其中每个编号都是一个7位1 ...

  3. 牛客网【每日一题】4月28日题目精讲 美味菜肴

    链接: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小明是个大厨,早上起来他开始一天的工作 ...

  4. 【牛客每日一题】tokitsukaze and Soldier 题目精讲 贪心、优先队列、堆

    链接:https://ac.nowcoder.com/acm/problem/50439 来源:牛客网 ACM在线模板 今天才发现牛客推出了一个每日一题的版块,3月25号就开始了,今天才发现,赶紧补救 ...

  5. 【每日一题】7月17日题目精讲—BOWL 碗的叠放

    [每日一题]7月17日题目精讲-BOWL 碗的叠放 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld ...

  6. 【每日一题】7月15日题目精讲—生日快乐

    [每日一题]7月15日题目精讲-生日快乐 [SCOI2009]生日快乐 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO For ...

  7. 【每日一题】7月13日题目精讲—Kingdom

    [每日一题]7月13日题目精讲-Kingdom 文章目录 题目描述 题解: 代码: 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 1048576K,其他语言2097152K 64bi ...

  8. 牛客网【每日一题】4月14日题目精讲 Xorto

    文章目录 题目描述 题解: 代码: 扩展 传送 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format:%lld 题目描述 ...

  9. 牛客网 【每日一题】8月5日题目精讲—蓝魔法师

    来源:牛客网: 文章目录 题目描述 题解: 代码: 题目描述 "你,你认错人了.我真的,真的不是食人魔."–蓝魔法师 给出一棵树,求有多少种删边方案,使得删后的图每个连通块大小小于 ...

最新文章

  1. 有了易生信,导师再也不用担心我的单细胞转录组整合分析啦
  2. php和mysql实现模糊查询_PHP MYSQL实现登陆和模糊查询两大功能_PHP
  3. js_sl 延迟菜单
  4. Pycharm的项目文件名是红色的原因及解决办法
  5. 谈大数据里各子项目搭建时的环境变量配置(深入)
  6. python环境准备_python开发环境准备
  7. 《TCP/IP详解卷1:协议》第3章 IP:网际协议(2)-读书笔记
  8. Asp.net MVC 仿照博客园的简单网站首页 列表设计
  9. 这家中国企业和星巴克对着干 年亏16亿却成为全球最快上市公司
  10. Zen Coding Visual Studio plugin 配置
  11. html 答题一题一题展示,在线答题系统如何展示考试结果
  12. C++ string字符串的增删改查
  13. ArchLinux安装配置
  14. Drool实战系列(二)之eclipse安装drools插件
  15. javamail 超时_为什么JavaMail连接超时过长
  16. 以太坊基础---奖励
  17. WINDOWS 耳机 有驱动 没声音
  18. Http——超文本传输协议
  19. 把之前写的一个小东西代码分享出来
  20. Sentinel限流熔断

热门文章

  1. 90后一代人还能通过攒钱改变现状吗?
  2. 笑到打鸣~ | 今日趣图
  3. 程序员8大终极杀器,你get了几个?
  4. linux 系统迁移到固态硬盘,把Debian GNU/Linux迁移到SSD上
  5. nginx php oracle,第8天 ORACLE安装及NGINX整合PHP环境
  6. linux e514写入错误,Linux上使用vim编辑文件保存时报错:E514: write error (file system full?)...
  7. java mongodb 批量删除_MONGODB删除/新增/更改大量记录的方法
  8. android arp工具,GitHub - SummerSnow274/ARP_sed_rev: 在Android通过ARP询问实现获取同一网络所有设备的MAC地址,AP隔离的网络除外...
  9. 7-3 树的同构 (25 分)(思路加详解)来呀baby!!!!!!!!
  10. 什么事计算机事实性知识,《人工智能》复习要点