【洛谷】P8685 [蓝桥杯 2019 省 A] 外卖店优先级 的题解
题目传送门
思路
这题一看大模拟,题上怎么说就怎么写,只不过需要优化,而且有一些注意点:
-
所求时刻 t t t 与每家店的最后一单时刻
last[i]
之间可能有差距,订单模拟结束后要注意计算这一段空档期的优先级。 -
如果订单只是一单一单处理是过不了所有的数据的,所以需要优化。
注意到同一时刻同一家店是可能有多个订单的,所以可以将这些订单一次性处理。
- 想一次性处理订单就会引出一个问题:如何判别订单可不可以同时处理。
这就需要将所有订单按主关键字为订单时间,副关键字为外卖店进行排序,所以可以考虑用 pair 来存放每一单订单。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
inline int read() {
register int x = 0, f = 1;
register char c = getchar();
while (c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
}
using namespace fastIO;
int n, m, t, last[100005], v[100005];
pair <int, int> order[100005];
bool res[100005];
int main() {
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n = read(), m = read(), t = read();
for(int i = 1; i <= m; i ++) {
order[i].first = read(), order[i].second = read();
}
for(int i = 1; i <= max(n, m); i ++) {
last[i] = 0, v[i] = 0, res[i] = false;
}
sort(order + 1, order + 1 + m);
for(int i = 1; i <= m; i ++) {
int name = order[i].second;
int ti = order[i].first;
int ans = 1;
while(name == order[i + 1].second && ti == order[i + 1].first) i ++, ans ++;
if(ti - last[name] > 0)
v[name] -= (ti - last[name] - 1);
last[name] = ti;
if(v[name] < 0) v[name] = 0;
if(v[name] <= 3) res[name] = false;
v[name] += (ans * 2);
if(v[name] > 5) res[name] = true;
}
for(int i = 1; i <= n; i ++) {
if(t > last[i]) {
v[i] -= (t - last[i]);
}
if(v[i] <= 3) {
res[i] = false;
}
if(v[i] > 5) {
res[i] = true;
}
}
int cnt = 0;
for(int i = 1; i <= n; i ++) {
if(res[i]) {
cnt ++;
}
}
write(cnt);
return 0;
}