POJ Problem_1193: 内存分配

1376B高效超短代码。

VS2017上可以跑,放在POJ就Time Limit Extended。

糟心啊,不应该啊。

#include <iostream>
#include <string>
#include <queue>

using namespace std;

class cProcess
{
public:
	cProcess();
	cProcess(int t, int m, int p);
	~cProcess();
	int T, M, P;
};
cProcess::cProcess(){}
cProcess::cProcess(int t, int m, int p) :T(t), M(m), P(p) {}
cProcess::~cProcess(){}
 
int CountSpace(const string& s) {
	int n = 0;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 0)++n;
	}
	return n;
}

int main()
{
	int N; cin >> N;
	queue<cProcess> qCin;
	int T, M, P;
	while (cin >> T >> M >> P) {
		if (T == 0 && M == 0 && P == 0)break;
		qCin.push(cProcess(T, M, P));
	}
	int nTime = 0, nQueueMax = 0;
	queue<cProcess> qQueue;
	string sStatus(N, 0);
	while (++nTime) {
		for (int i = 0; i < N; i++) { if (sStatus[i] > 0)--sStatus[i]; }
		if (qCin.size() > 0){
			if (qCin.front().T == nTime) {
				qQueue.push(qCin.front());
				qCin.pop();
			}
		}
		if (qQueue.size() > 0) {
			if ((CountSpace(sStatus)) >= qQueue.front().M) {
				while (qQueue.front().M--) {
					for (int i = 0; i < N; i++) {if (sStatus[i] == 0) { sStatus[i] = qQueue.front().P; break; }}
				}
				qQueue.pop();
			}
		}
		nQueueMax = (nQueueMax > qQueue.size()) ? nQueueMax : (int)qQueue.size();
		if (qCin.size() == 0) {
			bool flag = 1;
			int i = N;
			while (i--) {if (sStatus[i] != 0)flag = 0;}
			if (flag == 1)break;
		}
	}
	cout << --nTime << endl << nQueueMax << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值