1017. Queueing at Bank 解析

注意:后面来的顾客是有可能不用排队的。 比如11:00顾客没有了13:00来人了是不用排队的。

在选取窗口的时候方法和之前那个1014的选择方法不同。注意对比。


…………………………更新线………………………………

再做完1026后更新下思路。简化了很多的代码。这里只要是在17点之前到的都会服务,不管有多晚。

所以在求最小窗口时间的时候一定要注意,min的值给大一点。我开始设置成EndTIme一直报错。。

还有在更新窗口时间的时候注意是服务的时刻加上业务时间,别直接累加了。1026也是同样的问题。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MAX 10010
#define MAX2 110
#define starttime 3600 * 8
#define endtime 3600 * 17

using namespace std;

int n,k;

struct person{
	int arrive;
	int process;
	int wait;
};
person l[MAX];
int win[MAX2];

bool cmp(person p1,person p2){
	return p1.arrive < p2.arrive;
}

int sumwait = 0;

int main(){

	scanf("%d%d",&n,&k);

	for(int i = 0; i < n ;i++){
		string t_str;
		cin >> t_str >> l[i].process;
		if(l[i].process > 60){
			l[i].process = 60;
		}
		l[i].arrive = ((t_str[0]-'0') * 10 + (t_str[1]-'0'))* 3600 + 
			((t_str[3]-'0') * 10 + (t_str[4]-'0')) * 60 +
			(t_str[6]-'0') * 10 + (t_str[7]-'0');
	}

	sort(l,l+n,cmp);

	for(int i = 0;i < k;i ++){
		win[i] = starttime;
	}

	int c = 0;
	for(int i  = 0 ;i < n ;i++){
		if(l[i].arrive > endtime)
			break;
		else{
			c++;
			int minservetime = INT_MAX;
			int minwinno = 0;
			for(int j = 0; j < k ;j++){
				if(win[j] < minservetime){
					minservetime = win[j];
					minwinno = j;
				}
			}
			int servetime = max(minservetime,l[i].arrive);
			sumwait += (servetime - l[i].arrive);
			win[minwinno] = servetime + l[i].process * 60;
		}
	}

	printf("%.1f\n", sumwait / 60.0 /c);

	return 0;
}
………………………………完………………………………


#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

struct Node
{
	string time;
	int p;
};



int char2int(char c) {
	return int(c) - int('0');
}

int str2int(string s) {
	return (char2int(s[0]) * 10 + char2int(s[1])) * 3600 + (char2int(s[3]) * 10 + char2int(s[4])) * 60 + (char2int(s[6]) * 10 + char2int(s[7]));
}

bool cmp(Node N1, Node N2) {
	if (str2int(N1.time) < str2int(N2.time))
		return true;
	return false;
}


int FindWin(queue  * window, int K) { //找最优窗口
	int minI = 0;
	int minSize = window[0].size();
	for (int i = 0; i < K; i++) {
		if (minSize > window[i].size()) {
			minSize = window[i].size();
			minI = i;
		}
	}
	return minI;
}

int DeWin(queue  * window, int K) { //窗口出队
	int minI = 0;
	int minTime = window[0].front().p;
	for (int i = 0; i < K; i++) {
		if (minTime > window[i].front().p) {
			minTime = window[i].front().p;
			minI = i;
		}
	}
	for (int i = 0; i < K; i++) { //出队
		if (minTime == window[i].front().p)
			window[i].pop();
	}
	return minI;
}


int main( ){
	int N, K;
	cin >> N >> K;
	
	
	vector  tempNV;
	Node tempN;
	
	int TrueNum = 0;
	for (int i = 0; i < N; i++) {
		cin >> tempN.time >> tempN.p;
		if (str2int(tempN.time) <= 17 * 3600) {
			TrueNum++;
			if (tempN.p > 60)
				tempN.p = 60;
			tempNV.push_back(tempN);
		}		
	}

	N = TrueNum;
	Node * List = new Node[N];
	for (int i = 0; i < N; i++) {
		List[i] = tempNV[i];
	}
	
	sort(List, List + N, cmp);
	int * time = new int[N];//服务时间
	int * winTime = new int[K];//窗口计时
	int startTime = 8 * 3600; //开始时间
	int endTime = 17 * 3600 ; //结束时间
	int Num = 0;
	for (int i = 0; i < K; i++) {
		winTime[i] = startTime;
	}
	
	
	
	int SumWait = 0; //等待总时间
	int tempWin = 0; //选择窗口
	
	queue  * window = new queue[K];

	for (int i = 0; i < N; i++) {

		time[i] = str2int(List[i].time); //到达时间

		//tempWin = FindWin(window, K);
		//if (window[tempWin].size() < 1) {
		//	window[tempWin].push(List[i]);
		//}
		//else {
		//	tempWin = DeWin(window, K);
		//	window[tempWin].push(List[i]);
		//}
		int Min = winTime[0];
		tempWin = 0;
		for (int i = 0; i < K; i++) {
			//cout << winTime[i] << " ";
			if (Min > winTime[i]) {
				Min = winTime[i];
				tempWin = i;
			}
		}
		//cout << endl;
		//cout << tempWin << endl;
				
		if (winTime[tempWin] <= time[i]) {//窗口上次服务结束时间小于到达时间 无需等待
			winTime[tempWin] = time[i] + List[i].p * 60;
		}
		else{ //需要等待
			SumWait += (winTime[tempWin] - time[i]);
			winTime[tempWin] += List[i].p * 60;
		}
		//cout << "win = " << tempWin << endl;
		//cout << List[i].time << " " << List[i].p << endl;
		//cout << "WIN: " << winTime[tempWin] << " Arrive:" << time[i] << endl;
		//cout << SumWait << endl;
		//cout << endl;
	}

//	cout << SumWait << endl;
	if (N <= 0) cout << "0.0" << endl;
	else {
		float average = SumWait / 60.0 / TrueNum;
		printf("%.1f", average);
		cout << endl;
	}


	return 0;


}


你可能感兴趣的:(PAT)