Untitled

 avatar
Darin
plain_text
a year ago
5.6 kB
2
Indexable
Never
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
 
 
class UserSolution {
     
    class Passenger{
        int pId;
        int jId;
        int pPoint;
        public Passenger(int pId, int jId, int pPoint) {
            super();
            this.pId = pId;
            this.jId = jId;
            this.pPoint = pPoint;
        }
        public Passenger() {
            // TODO Auto-generated constructor stub
        }
        public int getpId() {
            return pId;
        }
        public void setpId(int pId) {
            this.pId = pId;
        }
        public int getjId() {
            return jId;
        }
        public void setjId(int jId) {
            this.jId = jId;
        }
        public int getpPoint() {
            return pPoint;
        }
        public void setpPoint(int pPoint) {
            this.pPoint = pPoint;
        }
         
         
    }
    class bymin implements Comparator<Passenger>{
 
 
        @Override
        public int compare(Passenger a, Passenger b) {
            // TODO Auto-generated method stub
            if(a.pPoint==b.pPoint)
                return b.pId-a.pId;
            else
                return a.pPoint-b.pPoint;
        }
         
    }
    class bymax implements Comparator<Passenger>{
 
 
        @Override
        public int compare(Passenger a, Passenger b) {
            // TODO Auto-generated method stub
            if(a.pPoint==b.pPoint)
                return a.pId-b.pId;
            else
                return b.pPoint-a.pPoint;
        }
         
    }
     
    //ArrayList<PriorityQueue<Passenger>> arrMax;
    ArrayList<PriorityQueue<Passenger>> arrMin;
    HashMap<Integer , Passenger>mapPass ;
    HashMap<Integer , ArrayList<Integer>>jtype;
    //ArrayList[] Section;
     
    int NOP;
    int NOS;
    int TOJ;
    int NOM;
     
    void init(int N, int M, int J, int mPoint[], int mJobID[])
    {
        NOP=N;
        NOS=N/M;
        NOM=M;
        TOJ=J;
        mapPass= new HashMap<Integer , Passenger>();
        jtype=new HashMap<Integer , ArrayList<Integer>>();
        arrMin= new ArrayList<PriorityQueue<Passenger>>();
         
         
        Passenger P;
        for (int i=0; i<N ;i++ )
        {
            P= new Passenger( i, mJobID[i], mPoint[i]);
            mapPass.put(i, P);
            for (int j=0; j<TOJ ;j++ )
            {
                ArrayList<Integer> L;
                if(mJobID[i]==j)
                {
                    if(jtype.get(j)==null)
                    {
                        L=new ArrayList<Integer>();
                        L.add(i);
                        jtype.put(mJobID[i], L);
 
 
                    }
                    else
                    {
                        L=jtype.get(j);
                        L.add(i);
                    }
                    break;
                }
            }
             
        }
 
 
        PriorityQueue<Passenger> PQ ;
        for(int i=0; i<NOS ;i++)
        {
             
            PQ=new PriorityQueue<Passenger>(new bymin());
            for(int j=i*M; j<(i+1)*M ;j++)
            {
                PQ.add(mapPass.get(j));
                 
            }
            arrMin.add(PQ);
        }
         
    }
 
 
    void destroy()
    {
    }
 
 
    int update(int mID, int mPoint)
    {
        Passenger P;
        if(mapPass.containsKey(mID))
        {
            P=mapPass.get(mID);
            P.setpPoint(P.getpPoint()+mPoint);
        }
         
        return mapPass.get(mID).getpPoint();
    }
 
 
    int updateByJob(int mJobID, int mPoint)
    {
        int sum=0;
        ArrayList<Integer> L;
        if(jtype.containsKey(mJobID))
        {
            L=jtype.get(mJobID);
            for(int i :L)
            {
                mapPass.get(i).setpPoint(mapPass.get(i).getpPoint()+mPoint);
                sum=sum+mapPass.get(i).getpPoint();
            }
        }
         
         
        return sum;
    }
 
 
    int move(int mNum)
    {
        int sum=0;
         
        PriorityQueue<Passenger> PQ2 ;
        Passenger[][]Pmax=new Passenger[NOS-1][mNum];
        Passenger[][]Pmin=new Passenger[NOS-1][mNum];
        for(int i=0; i<NOS-1 ;i++)
        {
            PQ2=new PriorityQueue<Passenger>(new bymin());
            PQ2.addAll(arrMin.get(i));
            arrMin.remove(i);
            arrMin.add(i, PQ2);
        }
         
        for(int i=0; i<NOS-1 ;i++)
        {    
            for(int k=0;k<mNum;k++)
            {
                    Pmin[i][k]=arrMin.get(i).poll();
                     
            }
        }
        for(int i=1; i<NOS ;i++)
        {
            PQ2=new PriorityQueue<Passenger>(new bymax());
            PQ2.addAll(arrMin.get(i));
            arrMin.remove(i);
            arrMin.add(i, PQ2);
        }
        for(int i=0; i<NOS-1 ;i++)
        {    
            for(int k=0;k<mNum;k++)
            {
                Pmax[i][k]=arrMin.get(i+1).poll();
                     
            }
        }
        for(int i=0; i<NOS-1 ;i++)
        {
            for(int k=0;k<mNum;k++)
            {
                arrMin.get(i).add(Pmax[i][k]);
                arrMin.get(i+1).add(Pmin[i][k]);
                sum=sum+Pmin[i][k].getpPoint()+Pmax[i][k].getpPoint();
            }
        }    
         
         
         
        return sum;
    }
}