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_1,p_2 = 2(\Sigma+T_1) \cdot D_2\\
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+2(\Sigma+T_1) \cdot D_2 \leq 2\Sigma \cdot D_2 + 2(\Sigma+T_2) \cdot D_1\\
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的更多相关文章

  1. BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花

    [题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...

  2. USACO 保护花朵 Protecting the Flowers, 2007 Jan

    Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...

  3. BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 448  So ...

  4. BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )

    考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...

  5. 1634: [Usaco2007 Jan]Protecting the Flowers 护花

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  So ...

  6. [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心

    1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 885  So ...

  7. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

  8. USACO Protecting the Flowers

    洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers 洛谷传送门 JDOJ 1009: 护花 JDOJ传送门 Description FJ出去砍木材去了,把N ...

  9. POJ 3262 Protecting the Flowers 贪心(性价比)

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7812   Accepted: ...

随机推荐

  1. selenium模块获得js动态数据-17track为例

    通过selenium模块驱动Chrome浏览器,获得js动态数据,以17track为例:通过运单号查询最新的物流信息 1 import re 2 from time import sleep 3 fr ...

  2. IDEA通过Jedis操作Linux上的Redis;Failed to connect to any host resolved for DNS name问题

    testPing.java public class testPing { public static void main(String[] args) { Jedis jedis = new Jed ...

  3. python学习-Day39-HTML-form表单

    目录 HTML-form表单 form表单 属性action input标签(输入元素) 文本域(Text Fields) 密码字段 单选按钮(Radio Buttons) 复选框(Checkboxe ...

  4. Bootstrap Blazor Table 组件(三)智能生成

    原文链接:https://www.cnblogs.com/ysmc/p/16201153.html Bootstrap Blazor 官网地址:https://www.blazor.zone 有了解过 ...

  5. Kubernetes生产环境最佳实践

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 众所周知,Kubernetes很难! 以下是在生产中使用 ...

  6. ts中 any、unknown、never 、void的区别

    any.unknown.never .void的区别 any 表示任意类型,设置为any相当于对该变量关闭了TS的类型检测.不建议使用 let a;(隐式any) //声明变量不赋值,就是any 等效 ...

  7. JUC自定义线程池练习

    JUC自定义线程池练习 首先上面该线程池的大致流程 自定义阻塞队列 首先定义一个双向的队列和锁一定两个等待的condition 本类用lock来控制多线程下的流程执行 take和push方法就是死等, ...

  8. 1903021121—刘明伟—Java第四周作业—java分支语句学习

    项目 内容 课程班级博客链接 19信计班(本) 作业要求链接 第四周作业 要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果). 扩展阅 ...

  9. Java注解Annotaton

    1.三种基本的Annotaton @Override : 限定某个方法,是重写父类方法 , 该注解只能用于方法 @Deprecated : 用于表示某个程序元素 ( 类 , 方法等 ) 已过时 @Su ...

  10. 【算法】堆排序(Heap Sort)(七)

    堆排序(Heap Sort) 堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法.堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父 ...