Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
4.8 kB
3
Indexable
Never
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeSet;
 
class UserSolution {
    class Ob {
        int P, C, ID;
 
        Ob(int p, int c, int id) {
            P = p;
            C = c;
            ID = id;
        }
    }
 
    HashMap<Integer, Ob> Ha = new HashMap<Integer, Ob>();
    Comparator<Ob> mComp = new Comparator<UserSolution.Ob>() {
        @Override
        public int compare(Ob arg0, Ob arg1) {
            return (arg0.P == arg1.P) ? arg0.C-arg1.C: arg0.P- arg1.P;
        }
    };
 
    TreeSet<Ob>[] Tr = new TreeSet[6];
    TreeSet<Ob> Temp = new TreeSet<Ob>(mComp);
 
    public void init() {
        Ha.clear();
        for (int i = 0; i < 6; ++i) {
            Tr[i] = new TreeSet<Ob>(mComp);
        }
        return;
    }
 
    public int add(char mStockInfo[]) {
        int i = 0, tP = 0, tC = 0, tT = 0, tB = 0;
        while (mStockInfo[i] != '\0') {
            if (mStockInfo[i] == '[') {
                switch (mStockInfo[i + 1]) {
                case 'P':
                    i += 9;
                    while (mStockInfo[i] != '}') {
                        tP = tP * 10 + mStockInfo[i] - '0';
                        i++;
                    }
                    i++;
                    break;
                case 'S':
                    i += 13;
                    while (mStockInfo[i] != '}') {
                        tC = tC * 10 + mStockInfo[i] - '0';
                        i++;
                    }
                    i++;
                    break;
                case 'B':
                    i += 11;
                    tB = mStockInfo[i] - '0' - 1;
                    i ++;
                    break;
                case 'T':
                    i += 8;
                    if (mStockInfo[i] == 'p') {
                        tT = 0;
                        i += 10;
                    } else {
                        tT = 1;
                        i += 7;
                    }
                    break;
                }
            }
            i++;
        }
        Ob tO = new Ob(tP, tC, tT * 3 + tB);
        Tr[tT * 3 + tB].add(tO);
        Ha.put(tC, tO);
        return Tr[tT * 3 + tB].last().C;
    }
 
    public int remove(int mStockCode) {
        if (!Ha.containsKey(mStockCode))return -1;
        Ob tO = Ha.get(mStockCode);
        Tr[tO.ID].remove(tO);
        return Tr[tO.ID].isEmpty()? -1:Tr[tO.ID].first().C;
    }
 
    public int search(char mCondition[]) {
        int i = 0, tP = 0, tC = 0;
        int[] tB = { -1, -1, -1 }, tT = { -1, -1 };
        while (mCondition[i] != '\0') {
            if (mCondition[i] == '[') {
                switch (mCondition[i + 1]) {
                case 'P':
                    i += 9;
                    while (mCondition[i] != '}') {
                        tP = tP * 10 + mCondition[i] - '0';
                        i++;
                    }
                    break;
                case 'B':
                    i += 11;
                    tB[0] = mCondition[i] - '0' - 1;
                    i++;
                    if (mCondition[i] == ',') {
                        i++;
                        tB[1] = mCondition[i] - '0' - 1;
                        i++;
                        if (mCondition[i] == ',') {
                            i++;
                            tB[2] = mCondition[i] - '0' - 1;
                            i++;
                        }
                    }
                    i++;
                    break;
                case 'T':
                    i += 8;
                    if (mCondition[i] == 'p') {
                        tT[0] = 0;
                        i += 10;
                        if (mCondition[i] == 'c') {
                            tT[1] = 1;
                            i += 7;
                        }
                    } else {
                        tT[0] = 1;
                        i += 7;
                        if (mCondition[i] == 'p') {
                            tT[1] = 0;
                            i += 10;
                        }
                    }
                    break;
                }
            }
            i++;
        }
         
        Ob tO = new Ob(tP, 0, 0);
        Temp.clear();
        for (i = 0; i < 3; i++) {
            if (tB[i] != -1) {
                Ob t=Tr[tT[0] * 3 + tB[i]].higher(tO);
                if(t!=null)Temp.add(t);
                if (tT[1] != -1) {
                    t=Tr[tT[1] * 3 + tB[i]].higher(tO);
                    if(t!=null)Temp.add(t);
                }
            }
        }
        return Temp.isEmpty()? -1:Temp.first().C;
    }
}
Leave a Comment