首页 > 专题系列 > Java解POJ > POJ 3262 Protecting the Flowers [解题报告] Java
2013
11-12

POJ 3262 Protecting the Flowers [解题报告] Java

Protecting the Flowers

问题描述 :

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.

输入:

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

输出:

Line 1: A single integer that is the minimum number of destroyed flowers

样例输入:

6
3 1
2 5
2 3
3 2
4 1
1 6

样例输出:

86

温馨提示:

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.

解题代码:

//* @author:
/*

有N头牛,每头牛有两个参数T和D。把第i头牛送到目的地要2*Ti的时间,在这期间其他牛会吃掉2*Ti*Di的花,问如何排放牛的顺序使得被吃到的花最少.
这是POJ上一道简单的贪心, 策略很简单,就是按每分钟吃掉的花从大到小排序,大的先运走。。。
*/


import java.util.*; 

class Cow implements Comparable{ 
    private long t;
    private long d;
   
     
    public long getT(){
     return this.t;
    }

    public long getD(){
      return this.d;
    }
    public Cow(long t,long d){ 
        this.t=t;
        this.d=d; 
       
    } 
     
    public int compareTo(Object o){ 
        Cow b = (Cow)o;
         double diff=1.0 * this.d / this.t - 1.0 * b.d / b.t;
        if (diff > 0) 
                return -1; 
            else if (diff == 0) 
                return 0; 
            else 
                return 1; 
         

    } 
     public String toString(){
       return ("("+t+"  "+d+")");
     }
   } 

public class Main{
   

   public static void main(String args[]){
    long ans=0,sum=0;
    
    Scanner in=new Scanner(System.in);
    int n=in.nextInt();
    Cow cow[]=new Cow[n];
    for(int i=0;i< n;i++){
      cow[i]=new Cow(in.nextLong(),in.nextLong());
        }
    Arrays.sort(cow);
    sum = 2 * cow[ 0 ].getT();
    ans = 0;
    for(int  i = 1; i < n; i ++ )
    {
        ans = ans + sum * cow[ i ].getD();
        sum = sum + cow[ i ].getT() * 2;
    }
    System.out.println(ans);
 }
}

  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 我还有个问题想请教一下,就是感觉对于新手来说,递归理解起来有些困难,不知有没有什么好的方法或者什么好的建议?