Protecting the Flowers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3204   Accepted: 1300

Description

Farmer John went to cut some wood and left N (2 ≤ N ≤ 100,000) cows eating the grass, as usual. When he returned, he found to his horror that the cluster of cows was in his garden eating his beautiful flowers. Wanting to minimize the subsequent damage, FJ decided to take immediate action and transport each cow back to its own barn.

Each cow i is at a location that is Ti minutes (1 ≤ Ti ≤ 2,000,000) away from its own barn. Furthermore, while waiting for transport, she destroys Di (1 ≤ Di ≤ 100) flowers per minute. No matter how hard he tries, FJ can only transport one cow at a time back to her barn. Moving cow i to its barn requires 2 × Ti minutes (Ti to get there and Ti to return). FJ starts at the flower patch, transports the cow to its barn, and then walks back to the flowers, taking no extra time to get to the next cow that needs transport.

Write a program to determine the order in which FJ should pick up the cows so that the total number of flowers destroyed is minimized.

Input

Line 1: A single integer N 
Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics

Output

Line 1: A single integer that is the minimum number of destroyed flowers

Sample Input

6
3 1
2 5
2 3
3 2
4 1
1 6

Sample Output

86

Hint

FJ returns the cows in the following order: 6, 2, 3, 4, 1, 5. While he is transporting cow 6 to the barn, the others destroy 24 flowers; next he will take cow 2, losing 28 more of his beautiful flora. For the cows 3, 4, 1 he loses 16, 12, and 6 flowers respectively. When he picks cow 5 there are no more cows damaging the flowers, so the loss for that cow is zero. The total flowers lost this way is 24 + 28 + 16 + 12 + 6 = 86.

Source

 
 
 
 
非常典型的贪心题,做起来也非常容易,很快就敲完了,主要卡我的地方在TLE。
是应该现在开始考虑一些这样的事了,N的范围为10的5次方,我第一次写的代码居然还用循环嵌套,这明显是TLE的命嘛。
其实一个小技巧完全可以避免双重循环,我居然没想到!!!!
嵌套的循环主要是在用来计算每头牛牵走后的剩余牛的破坏力,我居然模拟牵走过程,走掉一个牛,再模拟加一次破坏力。。很明显笨死了
 
明显每次计算的都是剩余牛的总破坏力,。。所以读数据的时候就顺便就把总破坏力存起来,。每走一头牛,就剪掉它对应的破坏力,这不就行了,一层循环搞定啊
 
其实不止是这个题目,这种小技巧其实跳出题目本身发现很简单,只是被题目的思维框住了,老想着去模拟一遍牵牛的过程。而且如果不是TLE的话,我还没发现这里有个优化方法。。。以后要尽量用这些优化方法来解题。
 
(我在TLE之后检查的时候,发现不仅数据量很大,数据本身可以累加到很大,所以把int 换成了__int64,..看discuss也发现确实要用64位。。之前都没意识到,还是太嫩了)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define maxn 100005
using namespace std;
struct cow{
int time;
int des;
}node[maxn];
bool cmp(cow a,cow b)
{
double t1=a.des*1.0/(a.time*1.0);
double t2=b.des*1.0/(b.time*1.0);
if (t1>t2)
return true; return false;
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
__int64 now=;
for (int i=;i<n;i++)
{
scanf("%d %d",&node[i].time,&node[i].des);
now+=node[i].des;
}
sort(node,node+n,cmp);
__int64 ans=;
//cout<<endl;
for (int j=;j<n-;j++)
{
now-=node[j].des;
ans+=now*node[j].time*;
// cout<<node[j].time<<" "<<node[k].des<<endl;
// cout<<ans<<endl<<endl;
} printf("%I64d\n",ans);
}
return ;
}

POJ-3262 贪心的一个小技巧的更多相关文章

  1. 快速掌握iOS API的一个小技巧

    快速掌握iOS API的一个小技巧 周银辉 iOS SDK和Developer Library中提供了各个类以及函数的帮助文档,这很棒,但要想了解整个库的大体结构(比如UIKit下有哪些类,他们的继承 ...

  2. 【flash】关于flash的制作透明gif的一个小技巧

    关于flash的制作透明gif的一个小技巧 或者说是一个需要注意的地方 1.导出影片|gif,得到的肯定是不透明的.2.想要透明背景,必须通过发布.3.flash中想要发布gif动画的话,不能有文字, ...

  3. 【每日一个小技巧】Python | input的提示信息换行输出,提示信息用变量表示

    [每日一个小技巧]Python | input的提示信息换行输出,提示信息用变量表示 在书写代码的途中,经常会实现这样功能: 请输入下列选项前的序号: 1.选择1 2.选择2 3.选择3 在pytho ...

  4. 针对ACM输出格式的一个小技巧(对格式错误说不!)

    printf("%d%c",bmax," \n"[i==n]); 上文中bmax为题目中需要输出的整形变量,可以脑补很多ans,max之类的,重点在于%c和后面 ...

  5. Scrapy 5+1 ——五大坑附送一个小技巧

    笔者最近对scrapy的学习可谓如火如荼,虽然但是,即使是一整天地学习下来也会有中间两三个小时的"无效学习",不是笔者开小差,而是掉进了深坑出不来. 在此,给各位分享一下作为一名S ...

  6. python笔记_查看函数调用栈的一个小技巧

    一.背景 最近在看一个开源框架的源码,涉及到的内容非常杂乱,有的函数不知道是在什么时候被谁给调用了?调用的时候传入了什么参数?为了解决这个问题,写了一个小的装饰器. 二.实现 这个装饰器函数主要参考了 ...

  7. Eclipse被卡死了或者失去响应了后分析根源的一个小技巧

    提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...

  8. C++模板类继承的一个小技巧

    先说一下background前段时间想实现一个Sqlite localstorage的功能,对应不同的Model 实体有不同的table, 每一次sql操作的函数签名中会有model实体中的struc ...

  9. 使用dwr时动态生成table的一个小技巧

    这篇随笔是我在07年写的,因为当时用了自己建设的blog,后来停止使用了,今天看到备份数据库还在,恢复出来放到这里.留着记录用. 我在使用DWR时,试了很多次都无法在动态生成的table中的一个或多个 ...

随机推荐

  1. centos 6.5安装NodeJS

    centos 6.5安装NodeJS 下载 可以在本地下载node.js最新版,然后通过ftp工具上传到服务器,或者直接在服务器终端使用wget命令下载(我当时下载的是node-v7.5.0-linu ...

  2. 浅谈JVM线程调度机制及主要策略

    在之前有说过线程,应该都知道,所谓线程就是进程中的一个子任务,一个进程有多个线程.今天的话主要就是谈一谈JVM线程调度机制.我们结合线程来说,当我们在做多线程的案例时,如一个经典案例,火车站卖票. * ...

  3. SQL语句利用日志写shell拿权限

    outfile被禁止,或者写入文件被拦截: 在数据库中操作如下:(必须是root权限) show variables like '%general%'; #查看配置 set global genera ...

  4. java.jvm调优

    _amazing~ 基本: 整理:

  5. 深度学习之常用linux命令总结

    深度学习中常用linux命令总结 1.创建文件夹 mkdir 文件名2.删除文件 rm -d 目录名 #删除一个空目录 rmdir 目录名 #删除一个空目录 rm -r 目录名 #删除一个非空目录 r ...

  6. 已解决!kali桌面无法放置快捷方式问题

    kali不知从哪个版本更新之后桌面上就不能添加快捷方式了,经过一番折腾找到了解决方法. 1.终端运行命令:apt-get install gnome-shell-extension-desktop-i ...

  7. Vue+Axios+Nigix+SpringCloud前端和后端搭建及其碰到的问题

    一.Axios.Router的安装和使用 1.如何安装Axios和Router 1).进入到工程所在的文件夹,通过cmd指令,进入到window的dos界面 2).输入:npm install axi ...

  8. Django(七)模型:字段属性、字段选项(参数)

    一.模型类属性命名限制 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/db/models/ 1)不能是python的保留关键字. 2)不允许 ...

  9. 使用Ubuntu系统管理包工具(apt)部署Zabbix企业级监控系统

    使用Ubuntu系统管理包工具(apt)部署Zabbix企业级监控系统  作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Ubuntu系统部署笔记:https://www.cnblo ...

  10. P1056 组合数的和

    P1056 组合数的和 转跳点: