Java实现 蓝桥杯VIP 算法提高 排队打水问题

算法提高 排队打水问题
时间限制:1.0s 内存限制:256.0MB
问题描述
  有n个人排队到r个水龙头去打水,他们装满水桶的时间t1、t2…………tn为整数且各不相等,应如何安排他们的打水顺序才能使他们总共花费的时间最少?
输入格式
  第一行n,r (n<=500,r<=75)
  第二行为n个人打水所用的时间Ti (Ti<=100);
输出格式
  最少的花费时间
样例输入
3 2
1 2 3
样例输出
7

数据规模和约定
  其中80%的数据保证n<=10

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;


public class 排队打水问题 {
	static Vector<Vector<Integer>> dui=new Vector<Vector<Integer>>();
	static void quickSoft(int start,int end,int[] s){
		if(start>=end)return;
		int l=start,r=end,p=s[end];
		while (l<r) {
			while(l<end&&s[l]<=p)l++;
			while(r>start&&s[r]>p)r--;
			if(l<r){
				s[l]^=s[r];
				s[r]^=s[l];
				s[l]^=s[r];
			}
		}quickSoft(start, l-1, s);quickSoft(l, end, s);
	}
	static int getMinindex(){
		int m=dui.get(0).get(dui.get(0).size()-1),index=0;
		for (int i = 1; i < dui.size(); i++) {
			int a=dui.get(i).get(dui.get(i).size()-1);
			if(a<m){m=a;index=i;}
		}
		return index;
	}
	static int getmin(){
		
		int num=0;
		for (int i = 0; i < dui.size(); i++) {
			
			for (int j = 0; j < dui.get(i).size(); j++) {
				num+=dui.get(i).get(j);
			}
		}
		return num;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		String[] a1=bf.readLine().split(" ");
		int n=Integer.parseInt(a1[0]);int r=Integer.parseInt(a1[1]);
		int[] s=new int[n];
		a1=bf.readLine().split(" ");
		for (int i = 0; i < s.length; i++) 
			s[i]=Integer.parseInt(a1[i]);
		quickSoft(0, n-1, s);
		for (int i = 0; i < r; i++) {
			dui.add(new Vector<Integer>());
			dui.get(i).add(0);
		}
		int index=0;
		for (int i = 0; i < s.length; i++) {
			index=getMinindex();
			//System.out.println(index);
			dui.get(index).add(dui.get(index).get(dui.get(index).size()-1)+s[i]);
		}
		System.out.println(getmin());
	}

}

posted @ 2019-06-22 09:35  南墙1  阅读(23)  评论(0编辑  收藏  举报