bzoj1634护花
试题描述:
约翰留下他的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护花的更多相关文章
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- BZOJ1634: [Usaco2007 Jan]Protecting the Flowers 护花
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 448 So ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
- 【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 ...
- 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 ...
- NOIP模拟赛-护花
[题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽 ...
- 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 ...
- bzoj1634 / P2878 [USACO07JAN]保护花朵Protecting the Flowers
P2878 [USACO07JAN]保护花朵Protecting the Flowers 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
随机推荐
- python基础===数据伪造模块faker
介绍文档: https://pypi.org/project/Faker/ https://faker.readthedocs.io/en/latest/ https://faker.readthed ...
- Mutex, semaphore, spinlock的深度解析
Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个.一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行. Semaphor ...
- 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL
为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备 python 2.7 scrapy框架的部署(可以查看上一篇博客的简 ...
- ubuntu中安装软件包问题 ------有一些软件包无法被安装。如果您用的是 unstable 发行版。。。
在ubuntu中安装软件包提示 有一些软件包无法被安装.如果您用的是 unstable 发行版,这也许是因为系统无法达到您要求的状态造成的.该版本中可能会有一些您需要的软件包尚未被创建或是它们已被从新 ...
- cenos6.5安装vsftp
1.安装 yum install vsftpd 2.配置 vi /etc/vsftpd/vsftpd.conf 检查是否如下配置 anonymous_enable=NO #禁止匿名访问 chroot_ ...
- mktime(将时间结构数据转换成经过的秒数)
mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime() ...
- Tomcat集群的session共享
配置Tomcat的session共享可以有三种解决方案: 第一种是以负载均衡服务器本身提供的session共享策略,每种服务器的配置是不一样的并且nginx本身是没有的. 第二种是利用web容器本身的 ...
- FPGA 17最佳论文导读 ESE: Efficient Speech Recognition Engine with Compressed LSTM on FPGA
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.机器学习技术感兴趣的同学加入. 后面陆续写一些关于神经网络加 ...
- (转)Opencv卷积操作
转自:http://www.2cto.com/kf/201312/267308.html Mask Operation filter2D函数 Last Edit 2013/12/24 所谓的Mask ...
- EasyUi – 2.布局Layout + 3.登录界面
1.页面布局 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.a ...