Algorithms improve handling of ice cubes

Resource limit
Memory limit: 256.0MB C/C++ time limit: 1.0s Java time limit: 3.0s Python time limit: 5.0s
Problem description
  Choufeng received a wonderful job: to carry ice cubes to the ice storage. Outside the ice storage There are N boxes of ice cubes. Due to the high outdoor temperature, the ice cubes will melt quickly, and the melting speed of each box of ice cubes is different. Because the volume and quality of each box of ice cubes are different, each box of ice cubes is transported into the ice cube The time spent is also different. Therefore, it is necessary to arrange the order of handling reasonably so as to minimize the total amount of ice melting. Ugly maple asks you to help calculate the minimum total amount of melting so that it can be reported to the superior. Input format Input the integer N in the first
line   and
  then
N lines down, two integers in each line, respectively represent the transport time Ti and melting speed Di of each box of ice cubes.
Output format
  Output the least total melting amount
Sample input
6
6 1
4 5 4
3 6
2 8
1
2 6
Sample output
86
Data size and convention
  2<=N<=100000, 1<=Ti<=4000000, 1<=Di<=100
Sample description
  Carry in the order of 6, 2, 3, 4, 1, 5

import java.util.*;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		Ice[] ice=new Ice[n];
		for(int i=0;i<n;i++) {
    
    
			ice[i]=new Ice(sc.nextInt(),sc.nextInt());
		}
		Arrays.sort(ice,new cmp());
		long sum=0,count=0;
		for(int i=n-1;i>=0;i--) {
    
    
			sum+=ice[i].d*count;
			count+=ice[i].t;	
			}
		System.out.print(sum);
	}
}
class cmp implements Comparator<Ice>{
    
    
		public int compare(Ice o1,Ice o2) {
    
    
			if(o1.t*o2.d<o2.t*o1.d)
				return 1;
			return -1;
		}
	}
class Ice{
    
    
		int t,d;
		Ice(int t,int d){
    
    
			this.t=t;
			this.d=d;
		}
}

Guess you like

Origin blog.csdn.net/qq_54537215/article/details/123929362