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 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
随机推荐
- 利用ANSYS进行橡胶坝的静力分析和模态计算
这个是我一个同学的毕业论文,我也帮了一点小忙,所以征得同学同意,把相关的经验共享一下(当时候做得也很艰难,网上查到的可参考的资料太少了,而且没有具体步骤). 先占一个位子,以前的模型还有命令流文件都找 ...
- http & https & http/2 & SPDY & WebSocket
参考文章: http https :https://www.jianshu.com/p/d286d097e56b https & ssl:https://www.jianshu.com/p/2 ...
- minikube配置CRI-O作为runtime并指定flannel插件
使用crio作为runtime后,容器的启动将不依赖docker相关的组件,容器进程更加简洁.如下使用crio作为runtime启动一个nginx的进程信息如下:根进程(1)->conmon-& ...
- linux下c++如何输入不回显
#include <stdio.h> #include <termios.h> #include <unistd.h> #include <iostream& ...
- ServiceStack.Redis简单封装
首先创建RedisConfig配置类 #region 单例模式 //定义单例实体 private static RedisConfig _redisConfig = null; /// <sum ...
- JDK相关目录介绍
JDK安装后会在硬盘生成一个目录,这个目录被成为JDK安装目录 bin目录: 该目录里用于存放一些可执行文件 .例如:javac.exe(java编译器),java.exe(java运行工具),jar ...
- Celery简介以及Django中使用celery
目录 Celery简介 消息中间件 任务执行单元 任务结果存储 使用场景 Celery的安装和配置 Celery执行异步任务 基本使用 延时任务 定时任务 异步处理Django任务 案例: Celer ...
- 将Redis设置为后台启动
Linux 在执行redis-server /etc/redis.conf 时默认开启的是一个前台的进程,也就是说启动的redis 就做不了其他的操作了,只有关闭redis 才能做其他的操作.非常的 ...
- [转]QT中的D指针与Q指针
Qt为了使其动态库最大程度上实现二进制兼容,引入了d指针的概念. 那么为什么d指针能实现二进制兼容呢? 为了回答这个问题,首先弄清楚什么是二进制兼容? 所谓二进制兼容动态库,指的是一个在老版本库下运行 ...
- RISC-V GNU 工具链:安装与使用
1. 安装Wmware和unbuntu,我安装的是Wmware workstation pro 12.1.1 build-3770994, unbuntu 是18.04.2 amd版本, ubuntu ...