POJ Protecting the Flowers
题目大意
奶牛要吃花,FJ来赶牛,将第i头牛赶走要2*ti分钟,奶牛每分钟吃di个单位花,求花的最小损失
先赶吃花多的,Wrong Answer QAQ
我们可以算一算损失
设sum=d1+d2+d3+…+dn
那损失为:
cost1=sum⋅2∑ni=1ti−(sum−d1)⋅2∑ni=2ti−(sum−d1−d2)⋅2∑ni=3ti...
如果将1号牛与2号牛交换
那损失为:
cost2=sum⋅2∑ni=1ti−(sum−d2)⋅2(t1+∑ni=3ti)−(sum−d1−d2)⋅2(∑ni=3ti)...
有变化的量为:t1·d2 –> t2·d1
假设cost1 > cost2
即 t1·d2 > t2·d1
所以t1d1>t2d2
所以在排序时,t1d1小的优先赶走
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
int t,d;
}a[100001];
bool cmp(node x,node y){return x.t*1.0/x.d<y.t*1.0/y.d;}
int getint()
{
int num=0,flag=1;char c;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
return num*flag;
}
int n;
long long ans,t;
int main()
{
int i;n=getint();
for(i=1;i<=n;i++)a[i].t=getint(),a[i].d=getint();
sort(a+1,a+n+1,cmp);
for(i=1;i<=n;i++)
{
ans+=a[i].d*t;
t+=2*a[i].t;
}
printf("%lld",ans);
}
POJ Protecting the Flowers的更多相关文章
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- 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 难得的信息课......来一题水题吧. 经典贪心题 我们发现,交换两头奶牛的解决顺序,对其他奶牛所产生的贡献并 ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
随机推荐
- codeforces 1183F 离散化枚举 约数定理
codeforces1183F 有技巧的暴力 传送门:https://codeforces.com/contest/1183/problem/F 题意: 给你n个数,要你从中选出最多三个数,使得三个数 ...
- SQL SQL Index SEEK VS Lookup
SEEK - find everything from index Lookup - find key from index, then visit table to find other row d ...
- windows下Qt编译Qtxlsx库和qtxlsx库的使用方法
最近接了个项目,合作的学长让用Qt写,而其中最重要的需求是将数据库的数据写入excel表格中和将excel的数据导入到数据库中,自己查阅了和多资料,最后决定使用qtxlsx开源库来操作excel,在编 ...
- Android5_浅谈Java的package机制
当代码量越来越大,类越来越多.尤其会增加同名类的风险.所以对类进行管理就显得非常重要. 包(package)机制是java中管理类的重要手段. 包名的命名方式:业内默认的做法是使用公司的网络域名的倒写 ...
- Hibernate各种查询方式及查询策略(转)
转自:https://www.cnblogs.com/xujingyang/p/6734203.html 在了解Hibernate的基本知识后,重点让我们看下相关查询方式和查询优化策略吧! 话不多说, ...
- python获取网页信息的三种方法
import urllib.request import http.cookiejar url = 'http://www.baidu.com/' # 方法一 print('方法一') req_one ...
- [梁山好汉说IT] 以水浒传为例讲解贝叶斯定理
0x00 摘要 看看呼延灼如何利用贝叶斯定理来判断 "自己是否是公明哥哥的心腹". 0x01 IT概念 1. 贝叶斯定理 贝叶斯定理是用来解决"逆概率"问题的, ...
- js对当前时间进行处理
//1.JS获取两个日期之间相差的天数 function getDaysBetween(dateString1, dateString2) { var startDate = Date.parse(d ...
- 20191010-4 alpha week 1/2 Scrum立会报告+燃尽图02
此作业链接参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/8747 2019小组名称:“组长”组 组长:杨天宇 组员:魏新,罗杨美慧,王 ...
- $[NOIp2015]$ 子串 $dp$
\(Sol\) 不知道为啥看起来就很\(dp\)的亚子.我们关心的只有\(A\)串当前用到哪一个,\(B\)串已经匹配到哪个位置,已经匹配的被分成了多少段.所以设\(f_{i,j,k,0/1}\)表示 ...