Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

附代码……很长,但是应该不乱,哪位大牛帮我看看么?

Posted by 150014 at 2007-11-28 17:32:20 on Problem 1193
In Reply To:话说我这题到底是WA掉了还是超时了? Posted by:150014 at 2007-11-27 20:35:34
#include<cstdio>
#include<deque>
#include<algorithm>
using namespace std;

const int INF=2000000000;

class Memory{
public:
	Memory();
	bool ReadSize();
	bool ReadTask();
	bool Print();
private:
	typedef pair<int,int> Block;
	struct Task{
		int blockSize,timeNeed,timeEnd;
		Block allocate;
	};
	int size,currentTime,exitTime,waitCount;
	deque<Block> pool;
	deque<Task> running;
	deque<Task> waiting;
	bool TaskBegin(Task);
	bool TaskEnd();
	int PoolUsable(Task);
	static bool BlockCompare(Block,Block);
	static bool TaskEndEarly(Task,Task);
};

Memory::Memory(){
	size=currentTime=exitTime=INF;
	waitCount=0;
}

bool Memory::ReadSize(){
	scanf("%d",&size);
	pool.push_back(Block(0,0));
	pool.push_back(Block(size,size));
	return true;
}

bool Memory::ReadTask(){
	Task current;
	int time;
	scanf("%d%d%d",&time,&current.blockSize,&current.timeNeed);
	current.allocate.first=current.allocate.second=current.timeEnd=INF;
	if(time==0 && current.blockSize==0 && current.timeNeed==0){
		while(!waiting.empty())
			TaskEnd();
		return false;
	}
	while(!running.empty() && time>=running.begin()->timeEnd)
		TaskEnd();
	currentTime=time;
	if(!TaskBegin(current)){
		waiting.push_back(current);
		waitCount++;
	}
	return true;
}

bool Memory::Print(){
	printf("%d\n%d\n",exitTime,waitCount);
	return true;
}

bool Memory::TaskBegin(Task newTask){
	newTask.allocate.first=PoolUsable(newTask);
	if(newTask.allocate.first==INF)
		return false;
	newTask.allocate.second=newTask.allocate.first+newTask.blockSize;
	pool.push_back(newTask.allocate);
	newTask.timeEnd=currentTime+newTask.timeNeed;
	if(exitTime==INF || exitTime<newTask.timeEnd)
		exitTime=newTask.timeEnd;
	running.push_back(newTask);
	sort(running.begin(),running.end(),TaskEndEarly);
	return true;
}

bool Memory::TaskEnd(){
	currentTime=running.begin()->timeEnd;
	while(!running.empty() && running.begin()->timeEnd==currentTime){
		pool.erase(find(pool.begin(),pool.end(),running.begin()->allocate));
		running.pop_front();
	}
	while(!waiting.empty() && TaskBegin(waiting.front()))
		waiting.pop_front();
	return true;
}

int Memory::PoolUsable(Task newTask){
	sort(pool.begin(),pool.end(),BlockCompare);
	for(int ptr=0;ptr<pool.size()-1;ptr++)
		if(pool[ptr+1].first-pool[ptr].second>=newTask.blockSize)
			return pool[ptr].second;
	return INF;
}

bool Memory::BlockCompare(Block left,Block right){
	return left.first<right.first;
}

bool Memory::TaskEndEarly(Task left,Task right){
	return left.timeEnd<right.timeEnd;
}

int main(){
	Memory test;
	test.ReadSize();
	while(test.ReadTask());
	test.Print();
	return true;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator