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 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
随机推荐
- Java ReentrantLock中tryLock与lock的区别(非公平锁与公平锁)
设置同步状态,利用CAS操作. // CAS操作:如果当前状态值等于期望值,则自动将同步状态设置为给定的更新值 protected final boolean compareAndSetState(i ...
- Redis(四)Pub/Sub
发布与订阅 Pub/Sub模式应该非常熟悉,在现实应用中被广泛的使用.如:微博中关注某个号,这个号有发新博时,关注的都会收到:github上watch了某个项目,当有issue时,就会发邮件. Red ...
- Java Automic包下的AtomicInteger
感谢这两位博主的文章,文章源于: https://www.cnblogs.com/chenpi/p/5375805.html https://blog.csdn.net/fanrenxiang/art ...
- 一些质量极高的project-based tutorials
<let's build a simple xxx> build your own lisp ★ Crafting Interpreters (学生版)Implementing Funct ...
- JAVA操作InfluxDB的一个Demo
一.基础连接类 package com.test.repository.utils; import com.dcits.domain.entry.bo.common.InfluxDbRow; impo ...
- redis持久化rdb和aof之间的优势劣势
1.RDB(Redis Database) a.基本概念 概念: 在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. Redis ...
- tkinter的单选Radiobutto
from tkinter import * def printSelection(): num = var.get() if num == 1: lab.config(text="你是男生& ...
- ES6迭代器
说起迭代器, 那就要先了解迭代模式 迭代模式: 提供一种方法可以顺序获得聚合对象中的各个元素, 是一种最简单, 也是最常见的设计模式,它可以让用户通过特定的接口寻访集合中的每一个元素 而不用了解底层的 ...
- ES6 Promise对象(七)
一.Promise介绍1.Promise简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果2.Promise可以将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函 ...
- Python渗透测试工具库
漏洞及渗透练习平台 WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.com ...