アルゴリズムJava実装--欲張りアルゴリズム--アクティビティの手配問題


アクティビティスケジューリング問題アルゴリズムのjava実装(欲張りアルゴリズム)
具体的な問題の説明とC/C++の実現はURLを参照してください
http://blog.csdn.net/liufeng_king/article/details/8709005
/**
 *       (    )
 * @author Lican
 *
 */
public class ActionOrder {
	public int greedySelector(int[] s,int[] f,boolean[] a){
		int n=s.length-1;
		a[1]=true;//        
		int j=1;
		int count=1;//        ,          
		for(int i=2;i<=n;i++){
			if(s[i]>=f[j]){//                        
				a[i]=true;
				j=i;
				count++;
			}
			else{
				a[i]=false;
			}
		}
		return count;
	}
	public static void main(String[] args) {
		int s[]={-1,1,3,0,5,3,5,6,8,8,2,12};//     1  (       ),   -1  
		int f[]={-1,4,5,6,7,8,9,10,11,12,13,14};
		boolean[] a=new boolean[s.length];
		ActionOrder ac = new ActionOrder();
		int counts=ac.greedySelector(s, f, a);
		System.out.println("              :"+counts);
		for(int i=1;i<=s.length-1;i++){
			if(a[i]){
				System.out.println(" "+i+"     ,      :"+s[i]+",     :"+f[i]);
			}
		}
	}
}
の実行結果は次のとおりです.