ccf 排队问题

来源:互联网 发布:阿里云域名使用方法 编辑:程序博客网 时间:2024/06/05 06:13
import java.util.*;public class Main {    public static void main(String[] args) {        new Main().run();    }    private void run() {        HashMap<Integer,Student> hashMap=new HashMap<Integer,Student>();        ArrayList<Student> arrayList=new ArrayList<Student>();        Scanner input=new Scanner(System.in);        int n,m;        n=input.nextInt();        m=input.nextInt();        for (int i =1; i <=n; i++) {            hashMap.put(i,new Student(i));        }        for (int i = 0; i < m; i++) {            int num=input.nextInt();            int ins=input.nextInt();            int low,high;            Student ptr=hashMap.get(num);            int index=ptr.position;            ptr.position+=ins;            if (ins>0){                low=index+1;                high=index+ins;                for (Integer integer : hashMap.keySet()) {                    if(hashMap.get(integer).position>=low&&hashMap.get(integer).position<=high)                    hashMap.put(integer,new Student(integer,hashMap.get(integer).position-1));                }                hashMap.put(ptr.id,new Student(ptr.id,ptr.position));            }            else {                low=index+ins;                high=index-1;                for (Integer integer : hashMap.keySet()) {                    if(hashMap.get(integer).position>=low&&hashMap.get(integer).position<=high)                    hashMap.put(integer,new Student(integer,hashMap.get(integer).position+1));                }                hashMap.put(ptr.id,new Student(ptr.id,ptr.position));            }        }        arrayList.addAll(hashMap.values());        Collections.sort(arrayList, new Comparator<Student>() {            @Override            public int compare(Student o1, Student o2) {                if (o2.position<=o1.position) return 1;                else return -1;            }        });        System.out.print(arrayList.get(0).id);        for (int i = 1; i <arrayList.size() ; i++) {            System.out.print(" "+arrayList.get(i).id);        }    }    class Student{        public int id;        public int position;        public Student(int i){            this.id=i;            this.position=i-1;        }        public Student(int id,int position){            this.id=id;            this.position=position;        }    }}
0 0
原创粉丝点击