17-09-02 (public key box *)

Keywords: Java CSP


The liver lasted eight hours. Finally straightened out the solution of this problem, and found the biggest problem recently. It's bad, it's delicious, but it's worth it.

Self summary

1. If you can't do it, don't think about whether multiple cycles work or not. Let's talk about it first.
2. I got 30 points for the first time, but the running time was 1.02 seconds. I was afraid at this time. In fact, the following methods even used three-tier for loops, but the cost of time and space was not much
3. Generally speaking, the idea is feasible when there are few points. The problem is that the requirements of the topic are not considered comprehensively (or, although they are considered, there are logical errors in the process of code implementation), or some boundary conditions are ignored.
4. Generally speaking, the test case is not very useful. Don't use it as a standard to debug your own code, but consider the overall situation.
5. Just like doing a topic, you should have ideas before writing, straighten out the logic before typing the code, and sharpen the knife without mistaking firewood (thanks to being too impatient)
6. After determining the idea, stick to the idea to the end. Don't want to change the idea when you encounter a small problem (otherwise you won't get anything)
7. Look at other people's codes, expand and accumulate more

Topic summary

1. There is no need to define multidimensional arrays. At the same time, the get method can be used to equivalent the get idea of map. Just create a class and place some attributes
2. Learn to use ArrayList
3. Mixed use of ArrayList and class
4. Sorting of classes

Problem solution

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        int K = input.nextInt();

      ArrayList<Teacher> teacher = new ArrayList<>();//teacher
      ArrayList<Integer> box = new ArrayList<>();//Place the key in use
        // Key initialization
        int[] key = new int[N];
        for(int i =0;i<N;i++){
            key[i] = i+1;

        }
        //Input teacher
        int maxtime=0,mintime=30;//Record time
        for(int i =0;i<K;i++){
            int k = input.nextInt();
            int start = input.nextInt();
            int end = input.nextInt();
            Teacher t = new Teacher(k,start,end);
            teacher.add(t);//Add to list
            if(mintime > start) mintime = start;
            if(maxtime < start+end) maxtime = start+end;
        }
        //Sort the teachers according to the initial time
//        for(int i =0;i<teacher.size()-1;i++){
//            for(int j =i+1;j<teacher.size();j++){
//                if(teacher.get(i).start > teacher.get(j).start){
//                       Teacher temp = teacher.get(i);
//                       teacher.set(i,teacher.get(j));
//                       teacher.set(j,temp);
//                }
//            }
//        }

        //Start traversal by time
        //System.out.print(mintime+" "+maxtime);
        for(int t=mintime;t<=maxtime;t++){
            for (Teacher value : teacher) {//Traversal teacher
                if (value.quit == t) {//It's time to return the key
                    box.add(value.w);
                }
            }
            //Sort the keys from small to large
            if(!box.isEmpty()) {
                for (int p = 0; p < box.size() - 1; p++) {
                    for (int q = p + 1; q < box.size(); q++) {
                        if (box.get(p) > box.get(q)) {
                            int temp = box.get(p);
                            box.set(p, box.get(q));
                            box.set(q, temp);
                        }
                    }
                }
                // Return the key
                for (int x = 0; x < key.length; x++) {
                    if (key[x] == 0) {//There is no key here
                        key[x] = box.get(0);//First element after sorting
                        box.remove(0);
                        if (box.isEmpty())
                            break;
                    }
                }
            }
            for (Teacher value : teacher) {//Traversal teacher
                if (value.start == t) {
                    for (int j = 0; j < key.length; j++) {
                        if (key[j] == value.w) {
                            key[j] = 0;
                            break;
                        }
                    }
                }
            }
            
        }

        for (int j : key) System.out.print(j + " ");
    }

}

class Teacher{
    int w;//Key number
    int start;//start time
    int end;//End time
    int quit ;//Return time
    public Teacher(int w,int start,int end){
        this.w = w;
        this.start = start;
        this.end = end;
        this.quit = start+end;
    }
}

Posted by mezise on Fri, 29 Oct 2021 06:52:21 -0700