zoukankan      html  css  js  c++  java
  • 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());
    	}
    
    }
    
    
  • 相关阅读:
    sizeof in C
    Get WIFI SSID and BSSID
    Swift和C混合Socket编程实现简单的ping命令&主机发现
    The different of bit Compiler
    Get all Ethernet information in Swift
    Get Local IP Address in Swift
    编译Unity3D Mono 加密DLL 填坑记
    spring-quartz 项目启动后执行一次job 之后按照规定时间执行job
    通过反射获取SSM的controller层的注解以及注解中的value值
    网页中高亮选中的关键字
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12948359.html
Copyright © 2011-2022 走看看