NC25043 [USACO 2007 Jan S]Protecting the Flowers
NC25043 [USACO 2007 Jan S]Protecting the Flowers
题目
题目描述
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 \(T_i\) minutes \((1 ≤ T_i ≤ 2,000,000)\) away from its own barn. Furthermore, while waiting for transport, she destroys \(D_i (1 ≤ D_i ≤ 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 × T_i\) minutes (\(T_i\) to get there and \(T_i\) 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, \(T_i\) and \(D_i\) , 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
题解
思路
知识点:贪心,排序。
注意到,交换某两头牛运送顺序,不改变其他结果。只要使任意两头牛的排序产生的毁坏最小,那么总毁坏将会是最小的,可以证明这是一个偏序关系,因此可以利用相邻两头牛的排序,来得到排序公式。
设 \(p_1,p_2\) 为相邻两牛毁坏的花, \(p_1',p_2'\) 为两牛逆序后毁坏的花,且 \(p_1+p_2 \leq p_1'+p_2'\)。
设之前所有牛的运送时间和为 \(\Sigma\) 。
所以有
p_1' = 2\Sigma \cdot D_2,p_2' = 2(\Sigma+T_2) \cdot D_1\\
\]
显然有,\(p_1 \leq p_2'\) 和 \(p_1' \leq p_2\) 。为了使 \(p_1+p_2 \leq p_1'+p_2'\) ,那么
2\Sigma \cdot (D_1+D_2)+2T_1D_2 \leq 2\Sigma \cdot (D_1+D_2)+2T_2D_1\\
T_1D_2 \leq T_2D_1
\]
因此按此排序,最后序列的毁坏最小。
时间复杂度 \(O(n \log n)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
struct pk {
int t, d;
}p[100007];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0;i < n;i++) {
cin >> p[i].t >> p[i].d;
}
sort(p, p + n, [&](pk a, pk b) {return a.t * b.d < b.t *a.d;});
long long sum = 0, time = 0;
for (int i = 0;i < n;i++) {
sum += time * p[i].d;
time += 2 * p[i].t;
}
cout << sum << '\n';
return 0;
}
NC25043 [USACO 2007 Jan S]Protecting the Flowers的更多相关文章
- BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
[题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...
- USACO 保护花朵 Protecting the Flowers, 2007 Jan
Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- 1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 493 So ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- USACO Protecting the Flowers
洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers 洛谷传送门 JDOJ 1009: 护花 JDOJ传送门 Description FJ出去砍木材去了,把N ...
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
随机推荐
- shell基础知识讲解
第1章 shell基础 1.1 什么叫做shell编程 shell编程也叫做bash高级编程语法 1.2 常见的shell命令解释器 bash redhat和centos使用 d ...
- 初学Java时没有理解的一些概念
背景 之前学Java属于赶鸭子上架,草草学习基础语法便直接做课程作业,许多概念问题仍不清楚,故在此梳理一下,主要参考廖雪峰和互联网资料. Java运行方式与JVM Java是介于编译型语言(C++)和 ...
- Revit二次开发之添加选项卡和按钮
我们日常在revit开发中经常会用到按钮,可以通过revitAPI提供的接口创建按钮,今天我简单介绍一下两种按钮,一种是单命令按钮,另一种是含下拉菜单的按钮,包括创建他们的方法. 实现方法 1.实 ...
- 这样理解 HTTP,面试再也不用慌了~
开源Linux 长按二维码加关注~ 上一篇:SSH只能用于远程Linux主机? 1 HTTP HTTP 协议是个无状态协议,不会保存状态. 2 Post 和 Get 的区别 先引入副作用和幂等的概念. ...
- vue - Vue脚手架/TodoList案例
今天做了一个案例,可以好好做做能够将之前的内容结合起来,最主要的是能对组件化编码流程有一个大概的清晰认知,这一套做下来,明天自己再做一遍复习一下,其实组件化流程倒是基本上没什么问题了,主要是很多vue ...
- dnf常用参数
dnf常用命令 检查并升级可用软件包: $ dnf update 删除缓存: $dnf clean all 列出可用的软件源: $ dnf repolist 搜索软件: $ dnf search $p ...
- NLP教程(7) - 问答系统
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/36 本文地址:http://www.showmeai.tech/article-det ...
- SpringCloud微服务实战——搭建企业级开发框架(四十):使用Spring Security OAuth2实现单点登录(SSO)系统
一.单点登录SSO介绍 目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的 ...
- EFCore常规操作生成的SQL语句一览
前言 EFCore的性能先不说,便捷性绝对是.Net Core平台下的ORM中最好用的,主要血统还百分百纯正. EFCore说到底还是对数据库进行操作,无论你是写Lamda还是Linq最后总归都是要生 ...
- Python的.gitignore模板
参考:https://github.com/github/gitignore Python的.gitignore模板,记录一下方便查询 # Byte-compiled / optimized / DL ...