/*
*日期:2010-04-19 22:02
*开发者:heroyan
*联系方式:zndxysf@126.com
*功能:带有限期的作业排序,假设每个作业都可在单位时间内完成,完成后获得一定的效益,求效益的最大值和作业顺序
*/
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
public class JobSort{
private static int MAX = 100;
private Job[] jbs = new Job[MAX];
private boolean[] visited = new boolean[MAX];//标志此时间是否已经被安排作业
private int[] target = new int[MAX];//目标编号
private double maxfit = 0.0;//最大收益
private int n;//作业个数
public JobSort(){}
public static void main(String args[]){
JobSort sp = new JobSort();
sp.init();
sp.js();
sp.print();
}
//初始化
public void init(){
Scanner scan = new Scanner(System.in);
int p,q;
double w;
System.out.println("Input the job number:");
n = scan.nextInt();
System.out.println("Input the time and benifit:");
for(int i = 1; i <= n; ++i){
jbs[i] = new Job();
jbs[i].no = i;
jbs[i].tt = scan.nextInt();
jbs[i].fit = scan.nextDouble();
}
maxfit = 0.0;
Arrays.fill(visited,false);
sort();
}
//按效益从大到小排序
public void sort(){
for(int i = 1; i < n; ++i){
for(int j = i+1; j <= n; ++j ){
if(jbs[i].fit < jbs[j].fit){
Job tmp = jbs[i];
jbs[i] = jbs[j];
jbs[j] = tmp;
}
}
}
}
//算法主体
public void js(){
int cnt = 0;
for(int i = 1; i <= n; ++i ){
if(!visited[jbs[i].tt]){
visited[jbs[i].tt] = true;
target[cnt++] = jbs[i].no;
maxfit += jbs[i].fit;
}
}
target[cnt] = -1;
}
//打印结果
public void print(){
for(int i = 0; target[i] > 0; ++i){
System.out.println("NO."+target[i]+" job is choosed");
}
System.out.println("maximum fit is "+maxfit);
}
}
class Job
{
public int no;//序号
public int tt;//期限
public double fit;//效益
}