只通过了测试用例,哎。。。

面向对象做的,没有简化代码,只通过了测试用例,哎。。。。不知道是不是卡在了时间复杂度。
可能还是面向对象太啰嗦。。
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        HashMap<Integer, List<User>> map = new HashMap<>();
        LinkedList<User> allUser = new LinkedList<>();
        Scanner sc = new Scanner(System.in);
        int maxFloor = 0;
        for (int i = 0; sc.hasNextInt(); i++) {
            int time = sc.nextInt();
            int floor = sc.nextInt();
            if (i==0){
                maxFloor = floor;
                continue;
            }
            User user = new User(i, time, floor,0);
            //加入到全局变量
            allUser.add(user);
            if (!map.containsKey(time))
                map.put(time,new LinkedList<>());
            map.get(time).add(user);
        }
        // 到现在就生成了 <时间,用户列表>的map
        Envirment envirment = new Envirment(map,maxFloor);
        envirment.run();

        // 运行完毕,看结果
        for (User user : allUser) {
            System.out.println(user.getArriveTime());
        }

    }

    static class User {
        int id;
        int time;
        int floorNum;
        int arriveTime;
        public User() {}
        User(int id, int time, int floorNum, int arriveTime) {
            this.id = id;
            this.time = time;
            this.floorNum = floorNum;
            this.arriveTime = arriveTime;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public int getTime() {
            return time;
        }
        public void setTime(int time) {
            this.time = time;
        }
        public int getFloorNum() {
            return floorNum;
        }
        public void setFloorNum(int floorNum) {
            this.floorNum = floorNum;
        }
        public int getArriveTime() {
            return arriveTime;
        }
        public void setArriveTime(int arriveTime) {
            this.arriveTime = arriveTime;
        }
    }

    static class Envirment {
        Elevator ele;
        Building building;
        HashMap<Integer, List<User>> map;

        Envirment(HashMap<Integer, List<User>> map, int maxFloorNum) {
            this.map = map;
            building = new Building(maxFloorNum + 1);
            ele = new Elevator();
            ele.setBuilding(building);
        }

        public void run() {
            int time;
            for (time = 0; true; time++) {
                // 1、首先根据时间来设置楼层和电梯的目标之类的
                if (map.get(time) != null && !map.get(time).isEmpty()) {
                    //      获取到当前时间点的人
                    List<User> curTimeUsers = map.remove(time);
                    //      把所有这个时间的人都放入到对应的楼层中
                    for (User curTimeUser : curTimeUsers) {
                        List<User> curFloorUsers = building.getFloors()[curTimeUser.getFloorNum()];
                        if (curFloorUsers == null) curFloorUsers = new LinkedList<>();
                        curFloorUsers.add(curTimeUser);
                        // 这个用户叫一下电梯。不管他反不反应
                        // 因为我们是按照编号入队列的,所以同一时间,一定是编号小的先bibibi
                        ele.bibibi(curTimeUser);
                    }
                }

                // 2、电梯运行一下
                ele.nextStep(time);
                //printStack(time);
                if (map.isEmpty() && floorNoUser() && ele.curUser.isEmpty()) {
                    break;
                }
            }
        }

        void printStack(int time) {
            System.out.println("-----------------第" + time + "秒\t" + "电梯在" + ele.getCurFloor() + "层\t" + "-------------------------");
            List<User>[] floors = building.floors;
            for (int i = 1; i < floors.length; i++) {
                List<User> floor = floors[i];
                System.out.println("第" + i + "层 " + (ele.getCurFloor() == i ? "*" : "") + floor);
            }
            System.out.println("----------------------------------------------------------------------");
        }

        /**
         * 判断楼道是否还有人在等电梯
         */
        boolean floorNoUser() {
            return ele.getWaitFloorNumList().isEmpty();
        }
    }

    static class Elevator {
        // 当前电梯状态
        byte status = Status.UP;
        // 当前电梯的等待队列
        LinkedList<Integer> waitFloorNumList = new LinkedList<>();
        int targetFloor = 0;

        // 当前电梯在几楼
        int curFloor = 1;
        // 当前电梯内的客户
        LinkedList<User> curUser = new LinkedList<>();
        Building building;

        // 加入到等待队列
        void bibibi(User user) {
            for (Integer waitFloorNum : waitFloorNumList) {
                if (user.floorNum == waitFloorNum)
                    return;
            }
            waitFloorNumList.addLast(user.floorNum);
        }

        void nextStep(int nowTime) {
            // 如果没人等电梯,直接跳过
            if (waitFloorNumList.isEmpty() && curUser.isEmpty()) return;

            if (targetFloor == 0) {
                targetFloor = waitFloorNumList.get(0);
                return;
            }

            switch (status) {
                // 如果空闲,直接不执行任何操作
                //case Status.FREE:   break;
                // 上行,直接自增楼层,如果到了目标楼层,切换成下行
                case Status.UP:
                    ++curFloor;
                    if (curFloor == targetFloor) {
                        pickUserUp();
                        status = Status.DOWN;
                    }
                    break;
                case Status.DOWN:
                    --curFloor;
                    // 把这一层的全部用户都接上
                    pickUserUp();
                    // 如果到了一楼,放下所有用户,并且给用户标志上到达时间
                    if (curFloor == 1) {
                        pickUserDown(nowTime);
                        status = Status.UP;
                        targetFloor = waitFloorNumList.isEmpty() ? 0 : waitFloorNumList.get(0);
                    }
                    break;
            }

        }

        // 把当前层的用户都接上电梯
        private void pickUserUp() {
            // 先把楼道的用户接上
            List<User> curFloorUsers = building.getFloors()[curFloor];
            curUser.addAll(curFloorUsers);
            curFloorUsers.clear();
            clearWaitInfo();
        }

        // 清空当前电梯坐在楼层的等待信息
        private void clearWaitInfo() {
            for (int i = waitFloorNumList.size() - 1; i >= 0; --i) {
                if (waitFloorNumList.get(i) == curFloor) {
                    waitFloorNumList.remove(i);
                }
            }
        }

        // 把当前层的用户都放到一楼
        private void pickUserDown(int nowTime) {
            for (int i = curUser.size() - 1; i >= 0; --i) {
                User user = curUser.get(i);
                user.setArriveTime(nowTime);
                curUser.remove(i);
            }
            //clearWaitInfo 已经删除了本楼层信息
            //waitFloorNumList.remove(0);
        }

        static class Status {
            static final byte UP = 1;
            static final byte DOWN = -1;
        }

        public byte getStatus() {
            return status;
        }

        public void setStatus(byte status) {
            this.status = status;
        }

        public LinkedList<Integer> getWaitFloorNumList() {
            return waitFloorNumList;
        }

        public void setWaitFloorNumList(LinkedList<Integer> waitFloorNumList) {
            this.waitFloorNumList = waitFloorNumList;
        }

        public int getTargetFloor() {
            return targetFloor;
        }

        public void setTargetFloor(int targetFloor) {
            this.targetFloor = targetFloor;
        }

        public int getCurFloor() {
            return curFloor;
        }

        public void setCurFloor(int curFloor) {
            this.curFloor = curFloor;
        }

        public LinkedList<User> getCurUser() {
            return curUser;
        }

        public void setCurUser(LinkedList<User> curUser) {
            this.curUser = curUser;
        }

        public Building getBuilding() {
            return building;
        }

        public void setBuilding(Building building) {
            this.building = building;
        }
    }

    static public class Building {
        // 每个楼层的人
        List<User>[] floors;
        @SuppressWarnings("unchecked")
        Building(int maxFloor){
            floors = new LinkedList[maxFloor];
            for (int i = 0; i < floors.length; i++) {
                floors[i] = new LinkedList<>();
            }
        }

        public List<User>[] getFloors() {
            return floors;
        }

        public void setFloors(List<User>[] floors) {
            this.floors = floors;
        }
    }
}


全部评论

相关推荐

杨柳哥:这不是普通人,那这个钱的是天才
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务