点击打开题目

题目大意

奶牛要吃花,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的更多相关文章

  1. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  2. POJ 3262 Protecting the Flowers 贪心(性价比)

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7812   Accepted: ...

  3. 【POJ - 3262】Protecting the Flowers(贪心)

    Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...

  4. poj 3262 Protecting the Flowers 贪心 牛吃花

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11402   Accepted ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. (摘录)ISO C++ Lambda表达式

    ISO C++ 11 标准的一大亮点是引入Lambda表达式.基本语法如下: [捕获列表](形参列表) mutable ->返回值类型 复合语句 其中除了"[]"(其中捕获列 ...

  2. HDU5618 Jam's problem again CDQ分治

    Jam's problem again CDQ分治 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5618 题意: \[ 有n 个元素,第 i 个元素有 ...

  3. Elasticsearch搜索调优

    最近把搜索后端从AWS cloudsearch迁到了AWS ES和自建ES集群.测试发现search latency高于之前的benchmark,可见模拟数据远不如真实数据来的实在.这次在产线的bac ...

  4. rapidxml遍历子节点例子

    <?xml version="1.0" encoding="utf-8"?> <luaword> <btn> <inf ...

  5. 【题解】CF986E Prince's Problem(树上差分+数论性质)

    [题解]CF986E Prince's Problem(树上差分+数论性质) 题目大意: 给定你一棵树,有点权\(val_i\le 10^7\).现在有\(m\)组询问给定参数\(x,y,w\)问你对 ...

  6. 洛谷$P2055\ [ZJOI2009]$ 假期的宿舍 最大流

    正解:最大流 解题报告: 传送门$QwQ$ 这种一看就很网络流鸭,直接说咋建图趴. 考虑把在校的人拆成人和床.$S$连向所有不回家的人,所有床连向$T$,认识的人之间人向床连边,跑个最大流就成. $o ...

  7. 使用EF操作Docker中的Mysql实例

    为啥我会选择mysql呢?因为我的服务器配置较低,而SqlServer在docker中的实例,服务器的运行内存应当保持在2G+,我没有这个条件,它会爆出这样的错误 sqlservr: This pro ...

  8. [MySQL实践] 实践记录

    [MySQL实践] 实践记录 版权2019.5.17更新 MySQL MySQL各版本区别 一.选择的版本 1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持 ...

  9. PHP 对接第三方 LINE 登录,网上找到相关的不多 但是网上哪些乱七八糟的啰啰嗦嗦 要么就是怎么做的, 什么步骤 总会给你省略, 如果有幸你看到我的 可以放心的复制即用, 当然 你也可以用postman去尝试 不过我觉得既然做开发 就没必要那个了! 如果用postman再最后一步的时候 请用本文最下方式

    * LINE 官方文档:https://developers.line.biz/en/docs/line-login/getting-started/* 开发者平台地址:https://develop ...

  10. 4.eclipse中导入别人用的源代码问题

    最近在导入别人用的源代码问题时,出现两个问题: 问题一:提示无法解析导入,如下图: 解决方法:删除项目下的module-info.java文件即可,或者在创建项目时将创建module-info.jav ...