洛谷 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. win10下MYSQL的下载、安装以及配置超详解教程(转)

    下载MYSQL 官网下载MYSQL5.7.21版本,链接地址https://www.mysql.com/downloads/.下载流程图如下: 进入官网点击Community,下载社区版. 找到MYS ...

  2. 使用Docker构建Jekyll框架网站

    使用Docker构建Jekyll框架网站 使用dockerfile构建apache + jekyll 目录 Jekyll基础镜像 构建Jekyll基础镜像 Apache镜像 构建Jekyll Apac ...

  3. 多项式 ln

    多项式 ln 定义 \(给一多项式F(x),求G(x)\equiv lnF(x)\pmod x^n\) 前置知识 \(不定积分\) \(微分\) \(多项式乘法逆\) 推式子: \[\because ...

  4. PyQt5笔记之标签

    标签 QLabel用于显示文本或图像.没有提供用户交互功能.标签的视觉外观可以通过各种方式进行配置,并且可以用于为另一个窗口小部件指定焦点助记键. 一个QLabel可以包含以下任意内容类型: 内容 设 ...

  5. Spring Boot入门及第一个案例

    一:SpringBoot是什么 springboot是对spring的缺点进行改善和优化,约定大于配置  开箱即用  没有代码生成 也无需xml 文件配置   可以修改属性值来满足需求 1) Spri ...

  6. scala的应用--UDF:用户自定义函数

    在window10下安装了hadoop,用ida创建maven项目. <properties> <spark.version>2.2.0</spark.version&g ...

  7. Linux内核调优部分参数说明

    #接收套接字缓冲区大小的默认值(以字节为单位). net.core.rmem_default = 262144 #接收套接字缓冲区大小的最大值(以字节为单位). net.core.rmem_max = ...

  8. winform子窗口与父窗口的交互-使用委托与事件

    实现子窗口与父窗口的交互,通过父窗口调用并控制子窗口,子窗口也能控制父窗口,使用委托和事件的方法,可以实现. 1.父窗口调用子窗口,并通过子窗口控制父窗口 新建工程,创建两个窗体    显示子窗体的代 ...

  9. 在普通网页显示正常,加Https报This request has been blocked; the content must be served over HTTPS.,https网站加载http资源时,http资源被block

    解决办法 :在html头加<meta http-equiv="Content-Security-Policy" content="upgrade-insecure- ...

  10. Dubbo(一):dubbo-spring-boot-starter

    dubbo-spring-boot-starter English Dubbo Spring Boot Starter.也可以关注dubbo官方的dubbo-spring-boot-project 支 ...