import java.util.*; public class Main { public static goods[] goodsList; public static ArrayList <activity> activityId; public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { /*//注意while处理多个case*/ int n = in.nextInt(); /*// goods*/ int m = in.nextInt(); /*// requests*/ int gid; int popular; int limitQuantity; goodsList = new goods[n]; activityId = new ArrayList<>(); for (int i = 0; i < n; i++) { gid = in.nextInt(); popular = in.nextInt(); limitQuantity = in.nextInt(); goods g = new goods(gid, popular, limitQuantity); goodsList[i] = g; } System.out.println(); for (int i = 0; i < m; i++) { int currentStamp = in.nextInt(); String requestType = in.next(); // 最后一个list读不出来,只能使用"list "在其后加一个空格才能读 switch (requestType){ case "add": { int res; int st = in.nextInt(); int et = in.nextInt(); int gi = in.nextInt(); int limit = in.nextInt(); if (st > currentStamp && st < et) { res = addActivity(st, et, gi, limit); } else res = -1; System.out.println(res); break; } case "buy": { int res; int activid = in.nextInt(); int quantity = in.nextInt(); res = buyGoods(currentStamp, activid, quantity); System.out.println(res); break; } case "list": { ArrayList<activity> activiId = getActivityList(currentStamp); if (activiId.isEmpty() || activiId == null) {System.out.println();} else { for (activity a: activiId ) { System.out.print(a.id+ " "); } System.out.println(); } break; } } } } in.close(); } public static int addActivity(int st, int et,int gid, int Quantity){ if (!activityId.isEmpty()){ for (activity a: activityId ) { if (a.go.gid == gid){ /*// 每个goodsId至多添加一次秒杀活动*/ return -1; } } goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ /*// goodsId 一定为存在的商品id。*/ if (goodsList[i].limitQuantity >= Quantity){ goods g = goodsList[i]; g.limitQuantity -= Quantity; /*// 修改商品参加活动后剩余库存*/ gForAct = new goods(gid, g.popular, Quantity); activity act = new activity(st, et, Quantity, gForAct); activityId.add(act); return act.id; }else { /*// 秒杀商品数量大于商品库存,添加失败,返回-1*/ return -1; } } } }else { goods gForAct = null; for (int i = 0; i < goodsList.length; i++) { if (gid == goodsList[i].gid){ /*// goodsId 一定为存在的商品id。*/ if (goodsList[i].limitQuantity >= Quantity){ goods g = goodsList[i]; g.limitQuantity -= Quantity; /*// 修改商品参加活动后剩余库存*/ gForAct = new goods(gid, g.popular, Quantity); activity act = new activity(st, et, Quantity, gForAct); activityId.add(act); return act.id; }else { return -1; } } } } return -1; } public static int buyGoods(int cuTime, int activid, int quantity){ if (activityId.isEmpty()){ return -1; }else { for (activity a: activityId ) { if (a.id == activid){ /*// activityId 一定是请求时存在的活动id*/ if (a.eti > cuTime && a.sti <= cuTime){ if (quantity <= a.go.limitQuantity){ /*// 购买成功,减少库存,返回0*/ a.go.limitQuantity -= quantity; a.lastSoldStamp = cuTime; /*// 更新最近一次卖出时间*/ return 0; }else return -1; /*// 购买数量大于秒杀商品剩余数量,购买失败,返回-1*/ }else return -1; /*// 秒杀未开始或已结束,购买失败,返回-1*/ } } } return -1; } public static ArrayList getActivityList(int cuTime){ if (activityId.isEmpty()) return null; ArrayList<activity> unSoldOut = new ArrayList<>(); ArrayList<activity> soldOut = new ArrayList<>(); ArrayList<activity> unStart = new ArrayList<>(); for (activity a:activityId ) { if (a.eti > cuTime){ if (a.sti <= cuTime){ if (a.go.limitQuantity > 0){ unSoldOut.add(a); /*//进行中(未售罄)*/ }else soldOut.add(a); /*// 进行中(已售罄)*/ }else unStart.add(a); /*// 未开始*/ }/*// 对于已结束的秒杀,不返回*/ } unSoldOut.sort( comparator1); /*// 多级排序*/ soldOut.sort( comparator2); unStart.sort(comparator3); activityId.clear(); activityId.addAll(unSoldOut); activityId.addAll(soldOut); activityId.addAll(unStart); return activityId; } private final static Comparator comparator1 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a1 = (activity) o1; activity a2 = (activity) o2; int a = a2.go.popular - a1.go.popular; /*// 依次按商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.gid - a1.go.gid; if (a != 0) cr = (a > 0) ? -2 : 2; else cr = 0; } } return cr; } }; private final static Comparator comparator2 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a2 = (activity) o1; activity a1 = (activity) o2; int a = a2.lastSoldStamp - a1.lastSoldStamp; /*// 按最后卖出时间从晚(lastSoldStamp 大)到早(lastSoldStamp 小),商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.popular - a1.go.popular; if (a != 0) cr = (a > 0) ? 2 : -2; else { a = (a2.go.gid - a1.go.gid); if (a != 0) cr = (a > 0) ? 1 : -3; else { cr = 0; } } } } return cr; } }; private final static Comparator comparator3 = new Comparator(){ @Override public int compare(Object o1, Object o2) { int cr = 0; if (o1 instanceof activity && o2 instanceof activity) { activity a2 = (activity) o1; activity a1 = (activity) o2; int a = a2.sti - a1.sti; /*// 依次按开始时间从早(sti 小)到晚(sti 大).商品人气值从高到低、商品ID从小到大排序*/ if (a != 0) cr = (a > 0) ? 3 : -1; else { a = a2.go.popular - a1.go.popular; if (a != 0) cr = (a > 0) ? 2 : -2; else { a = (a2.go.gid - a1.go.gid); if (a != 0) cr = (a > 0) ? 1 : -3; else { cr = 0; } } } } return cr; } }; static class goods{ /*// 商品*/ private int gid; private int popular; private int limitQuantity; public goods(int g, int p, int l){ gid = g; popular = p; limitQuantity = l; } } static class activity{ /*// 秒杀活动*/ private goods go; private static int num = 0; private final int id = num++; private int eti; private int sti; private int lastSoldStamp; public activity(int st, int et, int Quantity, goods g){ go = g; go.limitQuantity = Quantity; sti = st; eti = et; lastSoldStamp = eti-1; /*// 初始化最近一次卖出时间为最晚卖出*/ } } }
点赞 评论

相关推荐

无敌虾孝子:喜欢爸爸还是喜欢妈妈
点赞 评论 收藏
分享
牛客网
牛客企业服务