The priority Java easy-to-understand version of Lanqiao Cup Takeaway (80% score test mixed version)

Title description

There are N take-out shops maintained in the "Is it full" take-out system, numbered 1∼N. Each takeaway store has a priority, and the priority is 0
at the beginning ( at time 0 ).

After each time unit, if the takeaway shop has no order, the priority will be reduced by 1, and the lowest will be reduced to 0; if the takeaway shop has an order, the priority will not decrease but increase, and the priority will increase by 2 for each order.

If the priority of a takeaway restaurant is greater than 5 at a certain time, it will be added to the priority cache by the system; if the priority is less than or equal to 3, it will be cleared from the priority cache.

Given the M pieces of order information within time T, please calculate how many takeaway shops are in the priority cache at time T.

Input format

The first line contains 3 integers N, M, T. Each line of the following M lines contains two integers ts and id, indicating that the takeaway store with the number id at time ts received an order.

Output format

Output an integer to represent the answer.

data range

1≤N,M,T≤105,
1≤ts≤T,
1≤id≤N

Input sample:

2 6 6
1 1
5 2
3 1
6 2
2 1
6 2

Sample output:

1

Sample explanation

At 6 o'clock, the priority of No. 1 shop drops to 3 and is removed from the priority cache; the priority of No. 2 shop rises to 6, and the priority cache is added. So there is 1 store (No. 2) in the priority cache.

Ideas :

题目大概意思,N家外卖店,有订单的时候热度加2,没有订单的且热度不
为0的时候热度减1,热度大于5的时候时候进入优先缓存,小于等于3的时
候移除优先缓存,输出在T时刻时有多少店在优先缓存中。

1.用List来数组来存,每一个List new一个ArrayList()
	List array[]=new ArrayList[T+1];    //存放每个时刻对应的店铺
        for(int i=0;i<=T;i++){
    
    
            array[i]=new ArrayList();
        }
把每个时刻和对应的id输进去,再按时间从大到小来循环就不用排序了

2 同一时刻有多个订单,所以我们要循环一下,Lisr.remove之后看是否还
有订单,如果全部循环结束,就把没有订单的且热度大于1的店铺热度减1,
最后输出答案即可

Code

import java.util.*;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int M=input.nextInt();
        int T=input.nextInt();
        List arr[]=new ArrayList[T+1];
        int better[]=new int[N+1];
        boolean  level[]=new boolean[N+1];
        for(int i=0;i<=T;i++){
    
    
            arr[i]=new ArrayList();
        }
        for(int i=1;i<=M;i++){
    
    
            int ts=input.nextInt();
            int id=input.nextInt();
            arr[ts].add(id);    //t时刻有店铺id的订单,
        }
        for(int i=1;i<=T;i++){
    
    
            for(int j=1;j<=N;j++){
    
    
                int flag=0;
                Object k=j;
                while (arr[i].contains(k)){
    
    
                    flag=1;
                    
                    better[j]+=2;
                    arr[i].remove(k);
                    if(better[j]>5)
                        level[j]=true;
                }
                if(flag==0) {
    
        //此时刻无店铺j的订单
                    better[j] = better[j] == 0 ? 0 : better[j] - 1;
                    if (level[j]) {
    
        //原来在缓存队列
                        if (better[j] <= 3) {
    
        //优先级下降
                            level[j] = false;

                        }
                    }
                }


            }

        }
        int count=0;
        for(int i=1;i<=N;i++){
    
    
            if(level[i])
                count++;
        }
        System.out.println(count);
    }
}

If you want to know the full decomposition method, here is the link acwing moonlight

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/108227696