其他分享
首页 > 其他分享> > 洛谷——排队接水

洛谷——排队接水

作者:互联网

题目链接:排队接水

java代码:

import java.util.Arrays;
import java.util.Scanner;

class People implements Comparable<People>{
	int id;
	int time;
	
	public People(int id, int time) {
		super();
		this.id = id;
		this.time = time;
	}
//	升序排列
	public int compareTo(People newPeople) {
		return Integer.compare(this.time, newPeople.time);
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		int n;
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		
//		初始化对象数组并排序
		People arr[] = new People[n];
		for(int i=0;i<n;++i)
			arr[i] = new People(i+1,in.nextInt());
		Arrays.sort(arr);
		
//		该数组存放每个人需要等待的时间
		long sum[] = new long[n];
		sum[0] = 0;
//		在遍历时记录每个人需等待的时间,并处理第一行的输出
		for(int i=1;i<n;++i) {
			sum[i] = sum[i-1] + arr[i-1].time;
			if(i==1) System.out.print(arr[0].id + " ");
			System.out.print(arr[i].id);
			if(i!=n-1) System.out.print(" ");
			else System.out.println();
		}
		
//		累加求等待时间总和
		long res = 0;
		for(int i=0;i<n;++i) {
			res+=sum[i];
		}
		
		System.out.printf("%.2f",(double)res/n);
	}
}

标签:洛谷,People,int,排队,System,time,id,out
来源: https://blog.csdn.net/weixin_44668727/article/details/122792760