zoukankan      html  css  js  c++  java
  • POJ 3262 Protecting the Flowers 贪心(性价比)

    Protecting the Flowers
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7812   Accepted: 3151

    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

     
     
    题意:农夫John养的植物被cow吃了(John和cow的日常。。)现在给我们n组数,每组数分别表示赶走cow的单程时间和该cow每单位时间吃掉的植物数,求怎样赶cow使得损失最少。
    思路:贪心。先赶走的cow一定是当前牛群里,用时尽可能少,吃得尽可能多的一头,但不一定是最少和最多。所以可以把t和d以比值的方式记录下来(性价比),这样就可以同时把t、d作为参数来考虑。每次先赶当前t/d最小的,使得总损失最小。
     
     
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    
    struct Node{
        double t,d;
    }node[100005];
    bool cmp(Node a,Node b)
    {
        return (a.t/a.d)<(b.t/b.d);  //贪心
    }
    int main()
    {
        int n,i;
        long long sum,ans;
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++){
            scanf("%lf%lf",&node[i].t,&node[i].d);
            sum+=node[i].d;
        }
        sort(node+1,node+n+1,cmp); 
        ans=0;  
        for(i=1;i<=n;i++){
            sum-=node[i].d;
            ans+=sum*node[i].t*2;
        }
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    Android学习笔记_10_ContentProvider内容提供者的使用
    Android学习笔记_9_SQLiteOpenHelper对象之数据库增删改查以及事务回滚操作
    Android学习笔记_8_使用SharedPreferences存储数据
    Android学习笔记_7_使用 sax 或者 dom 或者 pull 解析XML文件
    Android学习笔记_6_保存文件到SDCard
    java GZIP 压缩数据
    struts2 实现文件下载方法汇总
    javascript ActiveXObject FileSystemObject 对象,创建、复制、删除、读取文件等
    css块元素的 display 属性 inline-block 的应用
    javascript操作Date对象
  • 原文地址:https://www.cnblogs.com/yzm10/p/7259573.html
Copyright © 2011-2022 走看看