USACO Protecting the Flowers
洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers
JDOJ 1009: 护花
Description
FJ出去砍木材去了,把N(2<=N<=100,000)头牛留在家中吃草,当他回来的时候,发现奶牛们都跑到花园里吃花去了,为了减少损失,FJ打算把牛移到牛棚中去。
每头牛的位置离牛棚需要Ti分钟(1<=Ti<=2,000,000),而且在等待被移走的过程中,每分钟破坏Di(1<=Di<=100)朵花,无论多么努力FJ一次只能移动一只奶牛,移动一只奶牛到牛棚需要2×Ti分钟(来回各一次)。
写一个程序安排移动顺序使得损失的花最少。
Input
第1行输入一个整数N
第2到N+1行每行包含两个整数Ti和Di
Output
输出一个整数表示最少损失的花的数量
Sample Input
6 3 1 2 5 2 3 3 2 4 1 1 6
Sample Output
86
HINT
【样例说明】 FJ按照6、2、3、4、1、5的顺序移走奶牛
题解:
贪心+数学推导。
这道题很容易能看出来是贪心,但是贪心策略不是那么容易得出。
我一开始想到的是,先移走吃花最多的牛,然后在吃花相同时移走用时最短的牛。
后来WA了82%。
所以我又想了想,所谓贪心是什么?
贪心就是把当前消耗最小的决策到最前面。
那么,我们消耗花最多的,其实并不是那头吃的最多的牛,而是吃的较多的牛每分钟吃的数量乘上他一共吃的时间!
我们假设A牛移动的时间为ta,吃的量为da,B牛移动的时间是tb,吃的量是db。
那么我们就得出这样的一个式子:
A的吃花总数为2tb*da.
B的总数就是2ta*db.
我们贪心的时候一定是想让它吃的最少,所以我们得出不等式。
2tb×da < 2ta×db
同时除以2
tb×da < ta×db
两边同时除以da,db。
tb/db < ta/da
所以我们得出了这个贪心策略就是按照时间除以量来排序,最后累计就可以。
(不开long long会WA 27%)
代码:
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
int n;
ll tot,ans;
struct node
{
ll t,d;
double temp;
}c[100001];
bool cmp(node a,node b)
{
return a.temp<b.temp;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&c[i].t,&c[i].d);
c[i].temp=(double)c[i].t/c[i].d;
tot+=c[i].d;
}
sort(c+1,c+n+1,cmp);
for(int i=1;i<=n;i++)
{
tot-=c[i].d;
ans+=c[i].t*2*tot;
}
printf("%lld",ans);
return 0;
}
USACO Protecting the Flowers的更多相关文章
- 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 ...
- USACO 保护花朵 Protecting the Flowers, 2007 Jan
Description 约翰留下了 N 只奶牛呆在家里,自顾自地去干活了,这是非常失策的.他还在的时候,奶牛像 往常一样悠闲地在牧场里吃草.可是当他回来的时候,他看到了一幕惨剧:他的奶牛跑进了他的花园 ...
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- POJ3262 Protecting the Flowers 【贪心】
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4418 Accepted: ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- 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 / P2878 [USACO07JAN]保护花朵Protecting the Flowers
P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
随机推荐
- 使用go-mysql-elasticsearch同步mysql数据库信息到ElasticSearch
本文介绍如何使用go-mysql-elasticsearch同步mysql数据库信息到ElasticSearch. 1.go-mysql-elasticsearch简介 go-mysql-elasti ...
- asp.net mvc移除X-AspNet-Version、X-AspNetMvc-Version、Server
asp.net mvc程序部署到IIS,,返回的HTTP头中包含Server, X-Powered-By, 和 X-AspNet-Version.X-AspNet-Version信息. 这些信息有时给 ...
- Kubernetes 学习(十)Kubernetes 容器持久化存储
0. 前言 最近在学习张磊老师的 深入剖析Kubernetes 系列课程,最近学到了 Kubernetes 容器持久化存储部分 现对这一部分的相关学习和体会做一下整理,内容参考 深入剖析Kuberne ...
- bash: telnet: command not found (Linux安装telnet)
问题描述: centos 系统没有 telnet 命令 bash: telnet: command not found 1.安装telnet服务 (3个) yum install xinetd tel ...
- MAST 397B: Introduction to Statistical Computing
MAST 397B: Introduction to Statistical ComputingABSTRACTNotes: (i) This project can be done in group ...
- layer弹出框,zIndex不断增加的问题
针对layer弹出框每次进行弹出操作时z-index不断加1的问题,手动设置过zIndex值不管用,每次关闭时清空layer对象也不管用. 解决办法: 修改layer.js,,将红框代码改为绿框代码, ...
- VisualHull && association4D 观摩记录
简单记录一下自己对VisualHull 和 association4D 两个程序的理解,没有别的意思(当然是真的 由于进度还很慢,暂时只是简单记录一下发现的点和踩过的坑,做完再把资料汇总. Visua ...
- Spring Cloud灰度发布之Nepxion Discovery
<蓝绿部署.红黑部署.AB测试.灰度发布.金丝雀发布.滚动发布的概念与区别> 最近公司项目在做架构升级,升级为 Spring Cloud,我们希望能够做到服务的灰度发布,根据访问量逐渐切换 ...
- parallels desktop 的新建虚拟机的步骤
新建虚拟机的图 点击窗口-----控制中心--再点击控制中心的+号
- app 崩溃测试 (转:CSDN 我去热饭)
首先,崩溃有几种情况: 闪退 提示停止运行 无响应 ( 不同情况虽然没有严格意义上区分开引起原因,但是都有侧重.在之后的工作中,我会实时补充统计.) 1.接口返回值 [直接原因]:app无法解析接口返 ...