POJ3262 Protecting the Flowers 【贪心】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4418 | Accepted: 1785 |
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
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
被long long坑了6次...贪心方法为每次选择的牛使得剩下的牛对花园破坏最小。
证明:若a,b为即将选择的两头牛,若果先选a的话。b牛的破坏是b.d*a.t,若先选b的话。a的破坏是a.d*b.t;无论a,b谁先谁后,其余牛的破坏都是(a.t+b.t)*other.d,所以仅仅须要依照先选择的牛使得剩下的牛对花园的破坏最小的标准对牛排序,再逐个选择就是了。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm> #define maxn 100005
typedef long long LL; struct Node {
int t, d;
friend bool operator<(const Node& a, const Node& b) {
return a.t * b.d < b.t * a.d;
}
} cow[maxn]; int main() {
int N, i, sumt = 0;
LL sumd = 0;
scanf("%d", &N);
for(i = 0; i < N; ++i)
scanf("%d%d", &cow[i].t, &cow[i].d);
std::sort(cow, cow + N);
for(i = 0; i < N; ++i) {
sumd += cow[i].d * sumt;
sumt += cow[i].t * 2;
}
printf("%lld\n", sumd);
return 0;
}
POJ3262 Protecting the Flowers 【贪心】的更多相关文章
- POJ 3262 Protecting the Flowers 贪心(性价比)
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7812 Accepted: ...
- poj 3262 Protecting the Flowers 贪心 牛吃花
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11402 Accepted ...
- poj 3262 Protecting the Flowers 贪心
题意:给定n个奶牛,FJ把奶牛i从其位置送回牛棚并回到草坪要花费2*t[i]时间,同时留在草地上的奶牛j每分钟会消耗d[j]个草 求把所有奶牛送回牛棚内,所消耗草的最小值 思路:贪心,假设奶牛a和奶牛 ...
- poj3262 Protecting the Flowers
思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- poj -3262 Protecting the Flowers (贪心)
http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...
- 【POJ - 3262】Protecting the Flowers(贪心)
Protecting the Flowers 直接中文 Descriptions FJ去砍树,然后和平时一样留了 N (2 ≤ N ≤ 100,000)头牛吃草.当他回来的时候,他发现奶牛们正在津津有 ...
- [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心
Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...
- BZOJ 1634: [Usaco2007 Jan]Protecting the Flowers 护花( 贪心 )
考虑相邻的两头奶牛 a , b , 我们发现它们顺序交换并不会影响到其他的 , 所以我们可以直接按照这个进行排序 ------------------------------------------- ...
- [BZOJ1634][Usaco2007 Jan]Protecting the Flowers 护花 贪心
1634: [Usaco2007 Jan]Protecting the Flowers 护花 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 885 So ...
随机推荐
- vue.js中的表单radio,select,textarea的v-model属性的用法
只要是表单元素,其值已经不会再用value来定义了,但是placeholder还是可以用来设置默认值. section1--input:type="text" type=" ...
- dockerfile note
dockerfile note reference summary defination docker can build images automatically by reading the in ...
- windows cmd 模仿电影黑客
1.win+R 2.输入cmd 3.按F11进入全屏 4.color a 改变颜色为绿色(可能看起来秀一点) 5.dir/s 查看所有文件,就跑起来了,看起来很酷,但是在懂得人眼里,没什么的(所以只能 ...
- POJ-3278 抓住这头牛
广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if else if,这样的话就直走一条路了. #include <ios ...
- CentOS 7 设置开机自启动
创建脚本: #!/bin/bash echo "hello!" # 启动虚拟环境 cd /data/env/crmenv/bin/ source activ ...
- CentOS 6.5 x64 安装Tomcat8 并配置两个Tomcat8
1.首先,安装tomcat的前提是已经配置好jdk环境变量,若没配好可以参考我的上一篇博文:CentOS 6.5 x64安装jdk8,当然也可以通过网络搜索安装步骤~~ 2.下载: 可以通过官网下载: ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- [转]ORA-38500: USING CURRENT LOGFILE option not available without stand
标签: oracle 10g 数据库 ora-38500 it 分类: IT author:skate time :2009/08/03 在dataguard启用实时恢复的时候,报如下错误: ORA- ...
- hdu 1500 dp
/* 状态转移方程式: dp[i][j]=Min(dp[i][j-1],dp[i-1][j-2]+(a[j-1]-a[j])*(a[j-1]-a[j])); 依次求出第i个人在第j个数时的所求的最小值 ...
- numpy模块
NumPy简介: NumPy 是高性能科学计算和数据分析的基础包:它是pandas等其他工具的基础. NumPy的主要功能: 1. ndarray,一个多维数组结构,高效且节省空间 (最主要的功能) ...