Java implementation of the Blue Bridge Cup VIP algorithm to improve queue to fetch water problem

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Improved algorithms for the enqueue kick problems
time limit: 1.0s memory limit: 256.0MB
Problem description
  has queued to r n individual taps fetch water, they fill the buckets time t1, t2 ............ tn and each is an integer not equal how should arrange their order to draw water to make them the least total time spent?
Input format
  The first line n, r (n <= 500 , r <= 75)
  the second n individual behavior kick elapsed time Ti (Ti <= 100);
output format of
  the least time consuming
sample input
. 3 2
. 1 2 3
sample output
7

Data size and conventions
  which 80% of the data to ensure that 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());
	}

}

Guess you like

Origin blog.csdn.net/a1439775520/article/details/93299670