POJ-3262 贪心的一个小技巧
| 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
Lines 2..N+1: Each line contains two space-separated integers, Ti and Di, that describe a single cow's characteristics
Output
Sample Input
6
3 1
2 5
2 3
3 2
4 1
1 6
Sample Output
86
Hint
Source
#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 贪心的一个小技巧的更多相关文章
- 快速掌握iOS API的一个小技巧
快速掌握iOS API的一个小技巧 周银辉 iOS SDK和Developer Library中提供了各个类以及函数的帮助文档,这很棒,但要想了解整个库的大体结构(比如UIKit下有哪些类,他们的继承 ...
- 【flash】关于flash的制作透明gif的一个小技巧
关于flash的制作透明gif的一个小技巧 或者说是一个需要注意的地方 1.导出影片|gif,得到的肯定是不透明的.2.想要透明背景,必须通过发布.3.flash中想要发布gif动画的话,不能有文字, ...
- 【每日一个小技巧】Python | input的提示信息换行输出,提示信息用变量表示
[每日一个小技巧]Python | input的提示信息换行输出,提示信息用变量表示 在书写代码的途中,经常会实现这样功能: 请输入下列选项前的序号: 1.选择1 2.选择2 3.选择3 在pytho ...
- 针对ACM输出格式的一个小技巧(对格式错误说不!)
printf("%d%c",bmax," \n"[i==n]); 上文中bmax为题目中需要输出的整形变量,可以脑补很多ans,max之类的,重点在于%c和后面 ...
- Scrapy 5+1 ——五大坑附送一个小技巧
笔者最近对scrapy的学习可谓如火如荼,虽然但是,即使是一整天地学习下来也会有中间两三个小时的"无效学习",不是笔者开小差,而是掉进了深坑出不来. 在此,给各位分享一下作为一名S ...
- python笔记_查看函数调用栈的一个小技巧
一.背景 最近在看一个开源框架的源码,涉及到的内容非常杂乱,有的函数不知道是在什么时候被谁给调用了?调用的时候传入了什么参数?为了解决这个问题,写了一个小的装饰器. 二.实现 这个装饰器函数主要参考了 ...
- Eclipse被卡死了或者失去响应了后分析根源的一个小技巧
提升程序员工作效率的工具/技巧推荐系列 推荐一个功能强大的文件搜索工具SearchMyFiles 介绍一个好用的免费流程图和UML绘制软件-Diagram Designer 介绍Windows任务管理 ...
- C++模板类继承的一个小技巧
先说一下background前段时间想实现一个Sqlite localstorage的功能,对应不同的Model 实体有不同的table, 每一次sql操作的函数签名中会有model实体中的struc ...
- 使用dwr时动态生成table的一个小技巧
这篇随笔是我在07年写的,因为当时用了自己建设的blog,后来停止使用了,今天看到备份数据库还在,恢复出来放到这里.留着记录用. 我在使用DWR时,试了很多次都无法在动态生成的table中的一个或多个 ...
随机推荐
- 使用 Helm【转】
Helm 安装成功后,可执行 helm search 查看当前可安装的 chart. 这个列表很长,这里只截取了一部分.大家不禁会问,这些 chart 都是从哪里来的? 前面说过,Helm 可以像 a ...
- 51nod 1441:士兵的数字游戏
1441 士兵的数字游戏 题目来源: CodeForces 基准时间限制:4 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 取消关注 两个士兵正在玩一个游戏,游戏开始的时 ...
- Redis Cluster 4.0.9 集群安装搭建
Redis Cluster 4.0.9集群搭建步骤:yum install -y gcc g++ gcc-c++ make openssl cd redis-4.0.9 make mkdir -p / ...
- c# copydata 消息
using PublicCode; using System; using System.Collections.Generic; using System.ComponentModel; using ...
- JAVA 怎么链接 SQL Server 数据库(半详细)
哇今天闲着没事准备把前几天链接数据库碰到的问题写到博客,写到一半浏览器切出去了,这个浏览器也没保存,晚上又写了一遍,唉 蛋疼呀╮(╯▽╰)╭!! 说一说连接数据库遇到的问题,唉问题繁多,原来自己刷的精 ...
- jmeter性能测试--浪涌测试
1. Ultimate Thread Group 右键测试计划-添加-Theads(Users)-Ultimate Thread Group,如下图1所示 图1 图2 参数说明,如下图 ...
- HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值. 分析:lazy标记主要体现在update上. 当l <= L && R <= r时,该结点的子结点 ...
- HihoCoder第十三周:最近公共祖先 一
#1062 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 同城那样神奇,但这个网站仍然让小Ho乐在其中,但这是为什么呢? "为什么呢?& ...
- flask邮箱注册问题
app/models.py self.confirmed = True db.session.add(self) db.session.commit() 这里的数据修改完后必须commit提交上去,不 ...
- 读取docx表格中的信息
参考了 http://blog.csdn.net/qq_34475777/article/details/62055523 http://www.cnblogs.com/deepwaterplan/a ...