试题描述:

约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小,约翰赶紧采取行动,把牛们送回牛棚. 牛们从1到N编号.第i只牛所在的位置距离牛棚Ti(1≤Ti≤2000000)分钟的路程,而在约翰开始送她回牛棚之前,她每分钟会啃食Di(1≤Di≤100)朵鲜花.无论多么努力,约翰一次只能送一只牛回棚.而运送第第i只牛事实上需要2Ti分钟,因为来回都需要时间.    写一个程序来决定约翰运送奶牛的顺序,使最终被吞食的花朵数量最小.

输入:

第1行输入N,之后N行每行输入两个整数Ti和Di

输出:

一个整数,表示最小数量的花朵被吞食

输入示例:

6
3 1
2 5
2 3
3 2
4 1
1 6

输出示例:

86

样例解释:

约翰用6,2,3,4,1,5的顺序来运送他的奶牛

解题思路:

我开始蒙按D从大到小排,一样的按T从小到大拍,结果只过了3个点(还过了三个点?!)然后打算好好地堆贪心公式。

我们来看i和i+1这两头牛。

Ti     Ti+1

Di    Di+1

如果i在前面,那么这两头牛所耗费的价值是2Ti*Di+1,如果i+1,则价值为2Ti+1*Di只要看这两个值哪个大,就把哪个排前面。

贪心公式出来了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
long long sum[];
long long n;
long long ans = ;
struct node
{
long long t, d;
int index;
}input[];
bool cnp(node a,node b)
{
return a.t*b.d<a.d*b.t;//贪心公式
}
int main()
{
freopen("flower.in", "r", stdin);
freopen("flower.out", "w", stdout);
cin >> n;
for (int i = ; i <= n; i++)
{
cin >> input[i].t >> input[i].d;
input[i].index = i;
}
sort(input + , input + n + , cnp);
for (int i = n; i >= ; i--)
sum[i] = sum[i + ] + input[i].d;
for (int i = ; i <= n; i++)
{
int k = input[i].t * ;
ans += k*sum[i + ];
}
cout << ans;
}

bzoj1634护花的更多相关文章

  1. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

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

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

  3. [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心

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

  4. 【bzoj1634】[Usaco2007 Jan]Protecting the Flowers 护花 贪心

    题目描述 Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the grass, a ...

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

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

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

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

  7. NOIP模拟赛-护花

    [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽 ...

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

    Description Farmer John went to cut some wood and left N (2 <= N <= 100,000) cows eating the g ...

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

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

随机推荐

  1. android 图片旋转 移动 放大缩小

    图片的变化主要是matrix的变化,对matrix不懂的可以先了解下matrxi. public class FunnyView extends View { /* * 手指按下时可能是移动 也可能是 ...

  2. arch安装完成之后不能使用笔记本自带的无线网卡

    问题描述如下 我笔记本的wifi网卡识别不了,不知道为什么?? 使用ifconfig -a 只列出了有线网卡以及外接的无线网卡如下 enp4s0f1: flags=<UP,BROADCAST,M ...

  3. python requests模块手动设置cookies的几种方式

    def use_cookie(self): cookies="YF-V5-G0=731b77772529a1f49eac82a9d2c2957f; SUB=_2AkMsEgief8NxqwJ ...

  4. 2017 CERC

    2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B: ...

  5. u-boot引导内核过程

    目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...

  6. python合并图片

    因项目需求需要将图片合并故写了一个python脚本,在这里放个笔记 #!/usr/bin/env python #coding=utf-8 import Image import os import ...

  7. 用户代码未处理EntityCommandExecutionmException报错解决方案

    原因可能是(1)没有编译好,清理解决方案,重新生成解决方案.          (2)可能是WebSiteConfiguration.DbProviderName;中为DbProviderName属性 ...

  8. 取消cp确认

    使用cp -r -f 强制覆盖拷贝命令时,每一个文件都需要认为的键入“Y”进行确认 可以通过取消同名cp的定义,命令如下unalias cp

  9. C# 消除累计误差的倒计时

    使用 C# 中自带的各种 timer 计时,都会有累计误差,以下代码实现了一种消除累计误差的方法,使得每次计时的误差,空值在 100 ms 以内(可以通过修改代码提升精度.) 对于精度要求在秒级别的简 ...

  10. Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)

    Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1…N,第i块地砖上积了fi英尺的雪 .在Farmer John的农舍的地窖中,总共有B双靴子,编号为1… ...