POJ-3262-Protecting the Flowers

Protecting the Flowers
Time Limit: 2000MS  Memory Limit: 65536K
Total Submissions: 4237  Accepted: 1710

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

USACO 2007 January Silver

 

 

题意:
有n个牛在FJ的花园乱吃。

所以FJ要赶他们回牛棚。

每个牛在被赶走之前每秒吃Di个花朵。赶它回去FJ来回要花的总时间是Ti×2。在被赶走的过程中,被赶走的牛就不能乱吃

思路:
贪心策略,对牛进行排序,排序的标准是,假设牛A与牛B要选一头赶走,我们首先要选择破坏最大的一头牛赶走,留破坏小
的牛。他们的破坏着呢麽计算呢?假设先赶走牛A,那么牛B造成的损失是2×TA×DB,先赶走牛B,那么牛A造成的损失是2×TA×DB,
所以,只要判断TA×DB与TA×DB谁大,就知道该先赶走谁了,所以数组排序的标准就是---Ti×Dj>Tj×Di

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x11111110
using namespace std;
struct node
{
   __int64 x;
   __int64 y;
}a[100010];
int cmp(node n,node m)
{
   return n.y*m.x>m.y*n.x;
}
int main()
{
    int i,j,n;
    __int64 sum,num;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            scanf("%I64d %I64d",&a[i].x,&a[i].y);
            sum+=a[i].y;
        }
        sort(a,a+n,cmp);
        num=0;
        for(i=0;i<n;i++)
        {
           sum-=a[i].y;
           num+=sum*(a[i].x*2);
        }
        printf("%I64d\n",num);
    }
    return 0;
}
时间: 2024-10-30 16:24:43

POJ-3262-Protecting the Flowers的相关文章

poj 1157 LITTLE SHOP OF FLOWERS dp

几个月没碰过算法,现在完全不会了. 再过几周就要考试了,抓紧时间恢复一下 这是个简单dp,之前一直以不会dp为借口躲避这些题目,其实就是太懒了不想想.但是逃避总是逃不过的,于是恢复训练就从dp开始 注意这题每个花都必须要取到,之前这一点错了很多次 简单可以写出这样的dp代码 </pre><pre name="code" class="cpp">#include <iostream> #include <cstdlib>

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

Protecting Your DHTML Using ASP

dhtml Protecting Your DHTML Using ASP        by Jean - Luc David    CATEGORIES:  Site Design, Scripting    ARTICLE TYPE: Tutorial Reader Comments         ABSTRACT     Article Rating       Useful          Innovative          Informative        100 res

POJ:DNA Sorting 特殊的排序

Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to

POJ 1001 Exponentiation 无限大数的指数乘法 题解

POJ做的很好,本题就是要求一个无限位大的指数乘法结果. 要求基础:无限大数位相乘 额外要求:处理特殊情况的能力 -- 关键是考这个能力了. 所以本题的用例特别重要,再聪明的人也会疏忽某些用例的. 本题对程序健壮性的考查到达了变态级别了. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/ 某人贴出的测试用例数据地址: http://poj.org/showmessage?message_id=76017 有

POJ 2240 Arbitrage:最短路 Floyd

Arbitrage:http://poj.org/problem?id=2240 大意: 给你m种货币,给你m种货币兑换规则,问通过这些规则最后能不能盈利.eg:1美元换0.5英镑,1英镑换10法郎,1法郎换0.21美元,这样1美元能换0.5*10*0.21=1.05美元,净赚0.05美元. 思路: 用Floyd找出每两种钱之间的最大兑换关系,遍历一遍,看有没有那种钱币最后能盈利,有就输出Yes,没有就是No.在处理钱币名称与编号之间的关系时,可以用map存(比较好用),当然也可以用字符串比较.

POJ 1860 Currency Exchange:最短路 Bellman-Ford

Currency Exchange:http://poj.org/problem?id=1860 大意:有多种货币,之间可以交换,但是需要手续费,也就是说既有汇率又有手续费.问经过交换之后能不能赚. 思路:Bellman_Ford,因为要求最长路,所以松弛条件改一下就好了. Tips: 3              2                  1                20.0货币的数量   兑换点的数量     主人公拥有的货币量    主人公拥有货币的价值1 2 1.00

POJ 1258 Agri-Net:最小生成树 Prim 模版题

Agri-Net:http://poj.org/problem?id=1258 大意:新镇长竞选宣言就是将网络带到每一个农场,给出农场个数,两两之间建光缆的耗费,求所有都联通的最小耗费. 思路:最小生成树,因为边比较稠密,用Prim做. PS:对于比较稠密的图,用Prim,对于比较稀疏的图,用 Kruskal.Kruskal是找边的过程,稀疏的话会比较快. 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

POJ 1789 Truck History:最小生成树 Prim

Truck History:http://poj.org/problem?id=1789 大意:用一个7位的string代表一个编号,两个编号之间的距离代表这两个编号之间不同字母的个数.一个编号只能由另一个编号变化的来,变化的字母的数量就是这两个编号之间相应的距离,现在要找出一个变化方案,使得总代价最小,也就是距离之和最小. 思路:将每个字符串当成一个节点,求出每个节点之间需要变化的次数为边的权值,用Prim建立最小生成树(稠密图). 更多精彩内容:http://www.bianceng.cnh