题目链接
http://poj.org/problem?id=1193
分析
模拟,可以用动态数组vector模拟内存情况,用队列queue模拟等待队列。
AC代码
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
inline int read() {
int num = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9')
num = num * 10 + c - '0', c = getchar();
return num;
}
struct Pro {
int t, m, p, s;
Pro(int t = 0, int m = 0, int p = 0) : t(t), m(m), p(p) {}
bool operator < (const Pro& rhs) const {
return s < rhs.s;
}
};
const int inf = 0x7fffffff;
int n, mint, cnt;
vector<Pro> p;
queue<Pro> q;
int insert(Pro x, int t) {
if (p.empty() || p[0].s >= x.m) {
x.t = t, x.s = 0;
p.push_back(x);
sort(p.begin(), p.end());
return 1;
}
for (int i = 1; i < (int)p.size(); ++i) {
if (p[i].s - (p[i - 1].s + p[i - 1].m) >= x.m) {
x.t = t, x.s = p[i - 1].s + p[i - 1].m;
p.push_back(x);
sort(p.begin(), p.end());
return 1;
}
}
int pp = p.size() - 1;
if (n - (p[pp].s + p[pp].m) >= x.m) {
x.t = t, x.s = p[pp].s + p[pp].m;
p.push_back(x);
sort(p.begin(), p.end());
return 1;
}
return 0;
}
void clear() {
int nmint = inf;
for (int i = 0; i < (int)p.size(); ++i) {
if (p[i].t + p[i].p == mint) p.erase(p.begin() + i--);
else nmint = min(nmint, p[i].t + p[i].p);
}
while (!q.empty() && insert(q.front(), mint)) {
nmint = min(nmint, mint + q.front().p);
q.pop(), ++cnt;
}
mint = nmint;
}
void work(int t, int m, int p) {
while (t >= mint) clear();
if (insert(Pro(t, m, p), t)) mint = min(mint, t + p);
else q.push(Pro(t, m, p));
}
int main() {
n = read();
int t, m, P;
while (scanf("%d%d%d", &t, &m, &P) == 3 && !(!t && !m && !P))
work(t, m, P);
while (!q.empty()) clear();
int ans = mint;
for (int i = 0; i < (int)p.size(); ++i)
ans = max(ans, p[i].t + p[i].p);
printf("%d\n%d", ans, cnt);
return 0;
}