POJ_3262_Protecting the Flowers(greedy)

Protecting the Flowers

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4950   Accepted: 1975

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.

题意:农夫养的N头牛在他的花园里吃花!这让农夫很苦恼,他一次只能把一头牛赶回牛栏,现在给你赶回每头牛到牛栏的时间Ti(也就是说来回要2*Ti的时间),以及这只牛每分钟能吃掉花的数量Di,求这些牛最少能吃掉多少花。

分析:贪心题。我们拿出两头牛a,b来考虑。若先把牛a赶走,那么吃掉的花的数量是2*(Ta)*(Db);若先把牛b赶回牛栏,那么吃掉花的数量是2*(Tb)*(Da)。所以最优化原则就是取min(2*(Ta)*(Db),2*(Tb)*(Da))。以此类推,贪心规则就出来了,按照此方法sort一遍就行了。

题目链接:http://poj.org/problem?id=3262

代码清单:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const int maxn = 100000 + 5;
const int maxv = 200000 + 5;

struct Point{
    int T;
    int D;
}point[maxn];

int N;

bool cmp(Point a,Point b){
    return a.D*b.T>a.T*b.D;
}

void input(){
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        scanf("%d%d",&point[i].T,&point[i].D);
    }
}

void solve(){
    sort(point,point+N,cmp);
    ll ans=0,t=0;
    for(int i=0;i<N;i++){
        ans=ans+(ll)point[i].D*t;
        t=t+(ll)(2*point[i].T);
    }
    printf("%I64d\n",ans);
}

int main(){
    input();
    solve();
    return 0;
}
时间: 2024-08-03 00:24:43

POJ_3262_Protecting the Flowers(greedy)的相关文章

Greedy:Protecting the Flowers(POJ 3262)

保护花朵 题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏的花最少? 一开始我做这道题的时候,用的是贪婪算法,然后我是这样做的,我先把D按照逆序排一遍,然后如果D相等的时候,再按T的顺序排序,然后两两比较看最小,就选谁,自以为是一个非常好的思路,但是果断WA了. 其实你要问我为什么这个思路?我一开始是这么想的,我想尽量让D大的元素出列,然后让T尽量少影响最

605. Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

HackerRank - &quot;Flowers&quot;

Apparently, more expensive flowers should be bought with lower coeff. (Greedy part), and any unbalanced assignment would cause extra cost. So the code would be: #include<iostream> #include<algorithm> using namespace std; int main(void) { int N

[leetcode]605. Can Place Flowers能放花吗

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

ZOJ 3794 Greedy Driver spfa

题意: 给定n个点,m条有向边,邮箱容量. 起点在1,终点在n,开始邮箱满油. 下面m行表示起点终点和这条边的耗油量(就是长度) 再下面给出一个数字m表示有P个加油站,可以免费加满油. 下面一行P个数字表示加油站的点标. 再下面一个整数Q 下面Q行 u v 表示在u点有销售站,可以卖掉邮箱里的任意数量的油,每以单位v元. 问跑到终点能获得最多多少元. 先求个每个点的最大剩余油量 f[i], 再把边反向,求每个点距离终点的最短路 dis[i]. 然后枚举一下每个销售点即可,( f[i] - dis

USACO Train 1.1.2 Greedy Gift Givers

这道题大意就是几个人互送礼物,让你求每个人的盈利. 原题给的样例数据: 5(人的个数.) =========(下面是人名,输出按照这顺序)davelauraowenvickamr ==========(下面是每个人的要给的人)dave200 3lauraowenvick ----------owen500 1dave ----------amr150 2vickowen -----------laura0 2amrvick ----------vick0 0 这题使用模拟算法就行了,就是注意输入

Greedy:Cow Acrobats(POJ 3045)

牛杂技团 题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小? 说实话这道题我一开始给书上的二分法给弄懵了,后来看了一下题解发现根本不是这么一回事,其实就是个简单的贪心法而已. 这题怎么贪心呢?就是按w+s从小到大排序就好了,证明一下: 1.先证明如果不满足w+s的序列性,无论谁在谁的上面,都会违反题设:(设A在B的上面) 如果 A.s+A.w<B.s+B.w 则如果B.s<m+A

ZOJ 3794 Greedy Driver

Greedy Driver Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3794 64-bit integer IO format: %lld      Java class name: Main Edward is a truck driver of a big company. His daily work is driving a truck from o

Flowers(二分水过。。。)

Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2579    Accepted Submission(s): 1265 Problem Description As is known to all, the blooming time and duration varies between different kinds