点击打开题目

题目大意

奶牛要吃花,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. Linux 内核提交和控制一个 urb

    当驱动有数据发送到 USB 设备(如同在驱动的 write 函数中发生的), 一个 urb 必须被 分配来传送数据到设备. urb = usb_alloc_urb(0, GFP_KERNEL); if ...

  2. Trie 树的一些题

    Trie 树的一些题 牛客练习赛11 假的字符串 (Trie树+拓扑找环) 链接:https://ac.nowcoder.com/acm/problem/15049 来源:牛客网 给定n个字符串,互不 ...

  3. C++模板特化与偏特化

    C++模板 说到C++模板特化与偏特化,就不得不简要的先说说C++中的模板.我们都知道,强类型的程序设计迫使我们为逻辑结构相同而具体数据类型不同的对象编写模式一致的代码,而无法抽取其中的共性,这样显然 ...

  4. Struts2 注释类型

    Struts 2 应用程序可以使用Java5注释作为替代XML和Java属性配置.这里是清单的不同的类别有关的最重要的注解: 命名空间注释(动作注释): @ Namespace注释允许在Action类 ...

  5. 并查集的超市问题---溜TM的

    三个月前我就错了,现在又错了,我就是个傻****** 服了,看图哇 打扰了... #include<cstdio> #include<iostream> #include< ...

  6. kotlin + springboot整合mybatis操作mysql数据库及单元测试

    项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...

  7. Ubuntu 18.04 + ROS Melodic + TurtleBot3仿真

    1. 下载安装包 官网地址: http://wiki.ros.org/action/show/Robots/TurtleBot?action=show&redirect=TurtleBot 所 ...

  8. 剑指Offer-3~9题

    3. 数组中重复的数字 题目描述: 在一个长度为 \(n​\) 的数组里的所有数字都在 \(0​\) 到 \(n-1​\) 的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个 ...

  9. JWT(二):使用 Java 实现 JWT

    JWT(一):认识 JSON WebToken JWT(二):使用 Java 实现 JWT 介绍 原理在上篇<JWT(一):认识 JSON Web Token>已经说过了,实现起来并不难, ...

  10. 常用Java静态代码分析工具的分析与比较

    给国产静态代码检测工具Pinpoint打Call! 简介 本文首先介绍了静态代码分析的基本概念及主要技术,随后分别介绍了4种现有的主流Java静态代码分析工具 (Checkstyle,FindBugs ...