洛谷——P2878 [USACO07JAN]保护花朵Protecting the Flowers
P2878 [USACO07JAN]保护花朵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 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.
有n头奶牛跑到FJ的花园里去吃花儿了,它们分别在距离牛圈T分钟处吃花儿,每分钟会吃掉D朵卡哇伊的花儿,(此处有点拗口,不要在意细节啊!),FJ现在要将它们给弄回牛圈,但是他每次只能弄一头回去,来回用时总共为2*T分钟,在这段时间内,其它的奶牛会继续吃FJ卡哇伊的花儿,速度保持不变,当然正在被赶回牛圈的奶牛就没口福了!现在要求以一种最棒的方法来尽可能的减少花儿的损失数量,求奶牛吃掉花儿的最少朵数!
输入输出格式
输入格式:
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
输入输出样例
6 3 1 2 5 2 3 3 2 4 1 1 6
86
说明
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.
写了个智障的贪心,结果发现贪错了、、(似乎仅过了样例)
智障的认为现将吃的多的迁回去花的时间一定少,其实不然,如果我们有这样两头牛,一个时间为1吃的为5,另一个迁回去所需的时间为6,吃的速度为7,如果我们要把第二头牛先迁回去,在这12秒内另一头牛吃了12*5,而反之先迁第一头牛那么我们只被吃了2*7的花,显然第二种是更优的。
既然这样我们考虑一下怎么贪心更优
我们发现如果我们有2 4,7 5,1 4,这样的几头牛,我们最先迁回去的一定是2 4,然后是7 5,然后在是1 4我们可以发现一个规律,c/t最大的一定是我们先迁回去的。
根据每分钟吃的花和运输时间的比值从大到小运...
如果先运j,i牛产生的贡献是cow[i].d*cow[j].t,如果先运i,j牛产生的贡献是cow[j].d*cow[i].t
假设前者比后者小,则,cow[i].d*cow[j].t<cow[j].d*cow[i].t,那么先运j更优,
更优的条件是,cow[i].d/cow[i].t<cow[j].d/cow[j].t为贪心的公式
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 110000
using namespace std;
long long n,tot,ans;
long long read()
{
,f=; char ch=getchar();
') ch=getchar();
+ch-',ch=getchar();
return x*f;
}
struct Node
{
long long t,c;
}a[N];
double cmp(Node a,Node b)
{
return a.c*1.0/a.t>b.c*1.0/b.t;
}
int main()
{
n=read();
;i<=n;i++)
a[i].t=read(),a[i].c=read(),tot+=a[i].c;
sort(a+,a++n,cmp);
;i<=n;i++)
{
tot-=a[i].c;
ans+=*a[i].t*tot;
}
printf("%lld",ans);
;
}
洛谷——P2878 [USACO07JAN]保护花朵Protecting the Flowers的更多相关文章
- 洛谷P2878 [USACO07JAN]保护花朵Protecting the Flowers
题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...
- bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers
P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
- P2878 [USACO07JAN]保护花朵Protecting the Flowers
一个类似国王游戏的贪心 话说要是先做了这个题,国王游戏之余懵逼这么久吗? #include<iostream> #include<cstdio> #include<alg ...
- Luogu_2878_[USACO07JAN]保护花朵Protecting the Flowers
题目描述 Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. ...
- USACO 保护花朵 Protecting the Flowers, 2007 Jan
Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...
- BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
[题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...
- 洛谷P2879 [USACO07JAN]区间统计Tallest Cow
To 洛谷.2879 区间统计 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. ...
- 洛谷 P3299 [SDOI2013]保护出题人 解题报告
P3299 [SDOI2013]保护出题人 题目描述 出题人铭铭认为给SDOI2012出题太可怕了,因为总要被骂,于是他又给SDOI2013出题了. 参加SDOI2012的小朋友们释放出大量的僵尸,企 ...
- 洛谷 P2879 [USACO07JAN]区间统计Tallest Cow
传送门 题目大意: n头牛,其中最高身高为h,给出r对关系(x,y) 表示x能看到y,当且仅当y>=x并且x和y中间的牛都比 他们矮的时候,求每头牛的最高身高. 题解:贪心+差分 将每头牛一开始 ...
随机推荐
- 搭建本地虚拟服务器linux(CentOS 7)的python虚拟环境(Hyper-V演示)
新建虚拟机->安装CentOS7->新建虚拟交换机:内部网络->CentOS7设置->网络适配器:虚拟交换机:新建虚拟交换机->进入CentOS # cd /etc/sy ...
- Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
文档:Docker 启动错误.note链接:http://note.youdao.com/noteshare?id=065111d506e1b132dc930dbe88f5d7b0&sub=A ...
- loj2276 「HAOI2017」新型城市化
给出的图是一个二分图(显然--吗),一个图的最大团=其补图的最大独立集,因此二分图的最大独立集就是补图的最大团. 欲使补图最大团变大,则要最大独立集变大.二分图最大独立集=点数-最小点覆盖.最小点覆盖 ...
- Python中__get__ ,__getattr__ ,__getattribute__用法与区别?
class C(object): a = 'abc' def __getattribute__(self, *args, **kwargs): print("__getattribute__ ...
- PHP-7.1 源代码学习:字节码生成 之 "$a = 1"
前言 本文通过分析 "$a=1" 这个 PHP 语句的编译和执行来窥探 php-cli 解释执行逻辑 准备 参考之前的系列文章,在 ubuntu 环境下下载,编译 PHP 源代码 ...
- C#-SqlServer连接
C#连接数据库在类方面没有java通用,不同数据库有不同的类库.在这里只做了SqlServer的连接类. public class DbLink { private string config = C ...
- [python][oldboy] * **的用法
* 和**主要用在函数的参数中, # coding=utf8 """ 三种编码: 1 python程序代码的编码 # coding=utf8 2 设置/查看python程 ...
- java EE技术体系——CLF平台API开发注意事项(1)——后端开发
前言:这是一篇帮助小伙伴在本次项目中快速进入到java EE开发的一些说明,为了让同组小伙伴们开发的时候,有个清晰点的思路.昨天给大家演示分享了基本概况,但没有留下文字总结说明,预防后期有人再次问我, ...
- No entity found for query异常
错误为getSingleResult();获取值时获取不到报异常. getSingleResult的源码有一句: @throws EntityNotFoundException if there is ...
- oracle列转行 WM_CONCAT LISTAGG
开发给个SQL说给某个条件时报ORA-22922 代码段: SELECT 袋号, SUM(实际重量) AS 实际重量, SUM(材积重量) AS 材积重量, COUNT(运单号) AS 件数, TO_ ...