洛谷 P2878 [USACO07JAN]保护花朵Protecting the Flowers

洛谷传送门

JDOJ 1009: 护花

JDOJ传送门

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的更多相关文章

  1. 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 ...

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

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

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

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

  4. POJ3262 Protecting the Flowers 【贪心】

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

  5. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

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

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

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

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

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

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

  9. bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers

    P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...

随机推荐

  1. Docker使用compose(原Fig)快速编配

    Docker使用compose(原Fig)快速编配 目录 安装 应用 构建以及运行 安装 在Linux上安装Fig: 在OS上安装: 在Linux上安装Fig: sudo bash-c "c ...

  2. Balking模式

    Balking模式讲的是如果现在不合适执行这个操作,或者没必要执行这个操作,就停止处理,直接返回 自动保存功能的实现逻辑一般都是隔一定时间自动执行存盘操作,存盘操作的前提是文件做过修改,如果文件没有执 ...

  3. Linux环境下如何计算CPU占用率【华为云技术分享】

    1.Linux 环境下查看 CPU 信息 1.1.查看 CPU 详细信息 通过 cat /proc/cpuinfo 命令,可以查看 CPU 相关的信息: [root@rh ~]$ cat /proc/ ...

  4. 【01】Saltstack:从零开始 Saltstack

    写在前面的话 最近一直都在整理以前的乱七八糟的笔记,所以会有很多老旧的东西都会被拉出来重新遛遛.算是再度系统的进行学习. 关于 Saltstack 的一些概念 Saltstack 是基于 Python ...

  5. 新一代ActiveMQ —— Apache ActiveMQ Artemis

    资料: .net demo : https://github.com/apache/activemq-artemis/tree/master/examples/protocols/amqp/dotne ...

  6. RESTful规范总结

    思维导图xmind文件:https://files-cdn.cnblogs.com/files/benjieming/RESTful%E8%A7%84%E8%8C%83.zip

  7. Windows Server2008R2,ServerWin2012 R2设置自动登录注册表配置

    serverWin2008 R2 2012自动登录一般是通过control userpasswords2 命令修改,其实注册表修改更简单.复制以下保存为xx.reg文件导入即可即可. Windows ...

  8. Spring扩展点之BeanFactoryPostProcessor

    前言 BeanFactoryPostProcessor接口是Spring中一个非常重要的接口,它的接口定义如下 public interface BeanFactoryPostProcessor { ...

  9. 安装ceres-solver win10遇到Eigen安装的问题

    1.无法打开包括文件: “Eigen/Core”: 去github上下载最新的源码 2.  "The Eigen/Array header does no longer exist in E ...

  10. laravel框架之状态更改

    //表单@if($v['sex']==0) <td class="se" ss="{{$v['sex']}}" id="{{$v['id']}} ...