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 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
随机推荐
- PurpleAir空气质量数据采集
PurpleAir空气质量数据采集 # -*- coding: utf-8 -*- import time, datetime, calendar import urllib, requests im ...
- 实战django(一)--(你也能看懂的)注册与登录(带前端模板)
先是具体目录:(主要是注意templates和static的位置),其中person文件夹是上一期实战的,不用理会,login是本节实战app 项目urls.py from django.contri ...
- Qt Quick 常用元素:RadioButton(单选框),CheckBox(复选框) 与 GroupBox(分组框)
先介绍一下 ExclusiveGroup. ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项.你可以在 ExclusiveGro ...
- Beta冲刺(7/7)——2019.5.29
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...
- Postman界面了解
Postman界面了解 2019年3月21日去面试了一家软件测试,本以为自己对简历上写的技能都熟悉,跳个槽,涨点工资,想象很美好,现实太残忍.当问到做接口测试postman和swagger工具的时候, ...
- MySQL 中获取用户表、用户视图、用户表中列信息
直接贴代码了: /// <summary> /// MySql 数据库维护中心 /// </summary> public class MySqlDbMaintenance:D ...
- [转] linux 查找文本过滤grep正则表达式命令详解用法
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用 ...
- 【学习笔记】C#中的泛型和泛型集合
一.什么是泛型? 泛型是C#语言和公共语言运行库(CLR)中的一个新功能,它将类型参数的概念引入.NET Framework.类型参数使得设计某些类和方法成为可能,例如,通过使用泛型类型参数T,可以大 ...
- C# 文件监听类 FileSystemWatcher 属性
属性: Path——这个属性告诉FileSystemWatcher它需要监控哪条路径.例如,如果我们将这个属性设为“C:Temp”,对象就监控那个目录发生的所有改变.IncludeSubDirecto ...
- Natasha V1.3.6.0 的升级日志
开源库满足于个人,而完善于大众. Natasha 自稳定版发布之后,众多老铁参与增强改进,感谢如下老铁的反馈: 1. 异常搜集 在 wenjq0911 建议下,添加了异常捕获,现 Natasha 的编 ...