[ccf 201709-2] Public Key Box

Keywords: Java

Test Name: Public Key Box
Time limit: 1.0s
Memory limit: 256.0MB
Description of the problem:
Problem Description
There is a school where teachers share N classrooms. As required, all keys must be placed in a public key box. Teachers cannot bring keys home.Every time before class, the teacher finds the key of his class from the public key box to open the door. After class, he puts the key back into the key box.
The key box has N hooks, arranged in a row from left to right, to hang keys for N classrooms.There are no fixed hanging positions for a string of keys, but there are signs on the keys, so teachers don't confuse the keys.
Each time they pick up a key, the teachers will find the key they need to remove it without moving the other keys.Each time a key is returned, the teacher who returns it finds the empty hook on the far left and hangs the key on it.If more than one teacher returns the keys, they return them in order of key number from smallest to largest.If both the teacher and the key are available at the same time, the teachers will return all the keys before taking them out.
At the beginning of the day, keys are placed in key boxes numbered from smallest to largest.There are K teachers to attend the class. Give each teacher the keys he needs, the time to start the class and the length of time to start the class. Assuming that the end of the class is the time to return the keys, what is the order of the keys in the final key box?
Input Format
The first line of input contains two integers, N and K.
The next K lines, each with three integers w, s, c, indicate the key number to be used by a teacher, the time at which the class begins, and the length of the class.There may be multiple teachers who use the same key, but the time they use the key does not overlap.
Ensure that the input data meets the input format without checking the validity of the data.
Output Format
Output a line containing N integers separated by a space representing the key number hung on each hook.
sample input
5 2
4 3 3
2 2 7
sample output
1 4 3 2 5
Sample Description
The first teacher used the key of Classroom 4 from time 3 and used 3 units of time, so she returned the key at time 6.The second teacher started using the key at time 2 and used 7 units of time, so she returned the key at time 9.
The key status after each critical moment is as follows (X means empty):
1 X345 after moment 2;
1 X3X5 after moment 3;
143X5 after moment 6;
1 4325 after moment 9.
sample input
5 7
1 1 14
3 3 12
1 15 12
2 7 20
3 18 12
4 21 19
5 30 9
sample output
1 2 3 5 4
Measure use case size and conventions
For 30% of the test cases, 1 < N, K < 10, 1 < w < N, 1 < s, c < 30;
For 60% of the test cases, 1 < N, K < 50, 1 < w < N, 1 < s < 300, 1 < c < 50;
For all evaluation cases, 1 < N, K < 1000, 1 < w < N, 1 < s < 10000, 1 < c < 100.

import java.awt.List;
import java.util.*;
/*
 * 
 * ccf20170917-2  Public Key Box
 * */

public class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[] key=new int[n];
        //initialize the key number
        for (int i = 0; i < n; i++) {
            key[i]=i+1;
        }
        int k = sc.nextInt();
        int[] usekey=new int[k];
        int[] start=new int[k];
        int[]  End=new int[k];
        int end=0;//end moment
        for (int i = 0; i < k; i++) {
            usekey[i]=sc.nextInt();
            start[i]=sc.nextInt();
            End[i]=sc.nextInt()+start[i];
            end=Math.max(end, End[i]);
        }

        LinkedList<Integer> list;
        int min = 0;
        int[] res=new int[n];
        for (int i = 0; i < key.length; i++) {
            res[i]=key[i];
        }
        for(int time=1;time<=end;time++) {
            //Return key
            //return the key
            list = new LinkedList<Integer>();
            int count=0;
            boolean whi=false;
            for (int i = 0; i < k; i++) {                               
                    if(End[i]==time) {
                        list.add(usekey[i]);
                        count++;    
                        whi=true;
                    }       
            }
            if(whi) {   
                    while(count>0) {
                        min=list.get(0);
                        for (int i = 0; i < list.size(); i++) {
                            min=Math.min(min, list.get(i));
                        }

                        for (int i = 0; i < list.size(); i++) { 
                            if(min==list.get(i)) {
                                int tempkey=0;
                                while(tempkey<=n) {
                                    if(res[tempkey]==0) {
                                        res[tempkey]=min;
                                        break;
                                    }
                                    tempkey++;
                                }
                                list.remove(i);
                            }
                        }               
                        count--;                    
                }
            }
            whi=false;

            //Get keys
            //take the key
            for (int i = 0; i < k; i++) {
                if(start[i]==time) {
                    for (int j = 0; j < res.length; j++) {
                        if(res[j]==usekey[i]) {
                            res[j]=0;
                        }
                    }
                }
            }
        }

        //output

        for (int i = 0; i < n; i++) {
            System.out.print(res[i]+" ");
        }               
    }
}

Posted by cmp241 on Sun, 19 May 2019 13:25:38 -0700