贪心法求解流水作业调度问题

#include
#include
#include
using namespace std;
#define max(x,y) ((x)>(y)?(x):(y))
#define N 100

int n=4;
int a[N]={5,12,4,8};
int b[N]={6,2,14,7};
struct NodeType{
	int no;
	bool group;
	int time;
	bool operator < (const NodeType &s) const {
		return time<s.time;
	}
}; 
int best[N];
int solve(){
	int i,j,k;
	NodeType c[N];
	for(i=0;i<n;i++){
		c[i].no=i;
		c[i].group=(a[i]<=b[i]);
		c[i].time=a[i]<=b[i]?a[i]:b[i];
	}
	sort(c,c+n);
	j=0;k=n-1;
	for(i=0;i<n;i++){
		if(c[i].group==1) best[j++]=c[i].no;
		else best[k--]=c[i].no;
	}
	int f1=0;
	int f2=0;
	for(i=0;i<n;i++){
		f1+=a[best[i]];
		f2=max(f2,f1)+b[best[i]];
	}
	return f2;
}
int main(){
	cout<<solve()<<endl;
	for(int i=0;i<n;i++){
		printf("%d ",best[i]+1);
	}
}

你可能感兴趣的:(算法)