ccf 排队问题

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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值