点击打开题目

题目大意

奶牛要吃花,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. 托管exe文件的加载和执行

    托管exe文件被启动的时候,首先被PE Loader载入.PE Loader载入exe文件之后,会分析PE文件头的data directory table,如果CLR_Header内的值不为0,表示该 ...

  2. asp.net core web api 发布到iis失败 错误500.19

    找了很久,发现是没有装DotNetCore.2.0.0-WindowsHosting.exe的原因. 还是官方文档最给力.部署时遇到问题的朋友可以参考官方文档 https://docs.microso ...

  3. C# 如何解析XML

  4. Team Foundation Server 2015使用教程【10】:团队项目删除

  5. 一点资讯 视频抓取 phantomjs

    # _*_ coding: utf- _*_ """ 思路: .列表页使用phantomjs模拟点击.每个链接只抓取第一页9-10条内容,按照标题去重 .布置定时任务,每 ...

  6. 王雅超的学习笔记-大数据hadoop集群部署(七)

    MySQL的安装部署

  7. form表单提交方式实现浏览器导出Excel

    刚开始使用ajax做Excel导出,发现ajax做不了浏览器导出只能下载到本地,于是用form提交可以提供浏览器下载Excel. 1>用ajax做本地下载: FileOutputStream f ...

  8. $POJ$2976 $Dropping\ tests$ 01分数规划+贪心

    正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...

  9. [UWP]XAML中的响应式布局技术

    响应式布局的概念是一个页面适配多个终端及不同分辨率.在针对特定屏幕宽度优化应用 UI 时,我们将此称为创建响应式设计.WPF设计之初响应式设计的概念并不流行,那时候大部分网页设计师都按着宽度960像素 ...

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

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