AcWing 1241. 外卖店优先级 (A不了的模拟)

外卖店优先级

⭐ 暴力枚举爆内存
⭐ 分析过程,优先级的 + - 具有片段性,要么整段+ ,要么整段 -

import java.io.*;
import java.util.*;

public class Main
{
	static int N = 100010, n, m;
	static int[] scores = new int[N];// 优先度
	static int[] last = new int[N];// 最近一次接受订单的时间
	static Pair[] orders = new Pair[N];// 订单数组,按时间排序
	static boolean[] st = new boolean[N];// 是否在优先队列

	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

	static class Pair implements Comparable<Pair>
	{
		int time;// 时间
		int id;// 店id

		public Pair(int time, int id)
		{
			super();
			this.time = time;
			this.id = id;
		}
		@Override
		public int compareTo(Pair p)
		{
			if (this.time == p.time)
			{
				return this.id - p.id;
			}
			return time - p.time;
		}
	}
	public static void main(String[] args) throws IOException
	{
		String[] ss = in.readLine().split(" ");
		n = Integer.parseInt(ss[0]);
		m = Integer.parseInt(ss[1]);
		int T = Integer.parseInt(ss[2]);

		for (int i = 0; i < m; i++)
		{
			ss = in.readLine().split(" ");
			int t = Integer.parseInt(ss[0]);
			int id = Integer.parseInt(ss[1]);
			orders[i] = new Pair(t, id);
		}
//		排序
		Arrays.sort(orders, 0, m);

//		
		for (int i = 0; i < m;)// 枚举每一个订单(可能连续多个订单对应一个店家)
		{
			int j = i;
			while (j < m && orders[i].time == orders[j].time && orders[i].id == orders[j].id)
			{
				j++; // 找到 同一 店家id 当前时刻的 所有订单(订单时间可能相同)
			}

			int id = orders[i].id;
			int t = orders[i].time;
			int cnt = j - i;// 从 i 到 j 都是 某一店家 在特定时间 的所有订单为 cnt

			i = j; // i 直接跳到 订单 j 的地方(orders[j] 不是当前店家当前时间段的订单)

			scores[id] -= t - last[id] - 1;// 当前 到 前一次有订单 的时间段
			if (scores[id] < 0)
				scores[id] = 0;// 优先值没有负值
			if (scores[id] <= 3)
				st[id] = false;

//			上边处理了前边的时间段 的 无订单的情况
//			下边处理当前的订单 + 的优先值
			scores[id] += cnt * 2;
			if (scores[id] > 5)
				st[id] = true;
			last[id] = t;
		}
//		处理最后一个订单到 结束之前的 时间段
		for (int i = 1; i <= n; i++)
		{
			if (last[i] < T)
			{
				scores[i] -= T - last[i];
				if (scores[i] <= 3)
					st[i] = false;
			}
		}
//		计算最终答案
		int res = 0;
		for (int i = 1; i <= n; i++)
		{
			if (st[i])
				res++;
		}
		System.out.println(res);
		in.close();
	}
}

posted @ 2023-03-21 23:30  兑生  阅读(16)  评论(0)    收藏  举报  来源
Live2D