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: ...
随机推荐
- 2022.02.27 CF811E Vladik and Entertaining Flags
2022.02.27 CF811E Vladik and Entertaining Flags https://www.luogu.com.cn/problem/CF811E Step 1 题意 在一 ...
- java的Integer中也会有缓存
在上篇<java的自动拆箱会发生NPE>博客中接收了java中的Integer中的自动拆箱产生的NPE,其实对于所有的包装类来说都是一样的,都会产生这样的问题,大家需要举一反三,做学问学知 ...
- MySQL 数据库备份脚本
MySQL 数据库备份脚本 #!/bin/bash # 数据库连接信息 DB_HOST="127.0.0.1" DB_PORT="3306" DB_USER=& ...
- Vite2+Vue3+ts的eslint设置踩坑
目录 新项目了 Vite搭建 eslint 先安装eslint 创建.eslintrc.js 引入规则 Airbnb 配合prettier 对ts的支持 .eslintrc.js 在页面上查看esli ...
- 聊一聊Redis的离线分析
背景 Redis 或许是我们大部分场景都会用到的一个利器,虽然是利器,用的姿势不对的话,终究还是会整出幺蛾子的. 比较常见的问题,不外乎内存暴增,慢查询等情况. 那么对于内存问题,可以借助redis自 ...
- 漏洞复现:MS12-020 远程桌面协议RDP远程代码执行漏洞
漏洞复现:MS12-020 远程桌面协议RDP远程代码执行漏洞 攻击机:Kali2019 靶机:Win7 64位 解题步骤: 1.打开Kali2019和Win7 64位 ,确定IP地址是多少 2.确定 ...
- 【链表】【leetCode高频】: 19. 删除链表的倒数第 N 个结点
1.题目描述 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. 2.算法分析 知识补充: . 分析: 题目要求是删除链表中倒数第N个结点.可以使用两个指针slow,fast. 重点是 ...
- Linux-流程控制语法-函数-属组
1.流程控制 1.1 条件选择 1.1.1选择执行if语句 格式: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ] ... ...
- 小白必看:零基础安装Linux系统(超级详细)
我们以最新发布的CentOS 8.1为例,学习下如何安装Linux系统 准备工作: 1.一台可以访问互联网的电脑 2.VMware Workstation安装包 3.CentOS8.1镜像(CentO ...
- UDP协议,多道技术,进程,同步与异步,阻塞与非阻塞
UDP协议 简介 UDP叫做用户数据报协议,是OSI七层参考模型中传输层使用的协议,他提供的是不可靠传输,既它在传输过程 中不保证数据的完整性! 端口号 UDP使用IP地址和端口号进行标识,以此将数据 ...