关注
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; /*// 初始化最近一次卖出时间为最晚卖出*/
}
}
}
查看原帖
点赞 评论
相关推荐
牛客热帖
正在热议
# 25届秋招总结 #
351780次浏览 3429人参与
# 我的实习求职记录 #
6084712次浏览 83676人参与
# 百度开奖 #
213202次浏览 1349人参与
# 地方国企笔面经互助 #
5196次浏览 13人参与
# 职场吐槽大会 #
90635次浏览 752人参与
# 选完offer后,你后悔学本专业吗 #
22976次浏览 165人参与
# 北方华创开奖 #
49810次浏览 448人参与
# ai智能作图 #
3139次浏览 66人参与
# 运营商笔面经互助 #
92624次浏览 1334人参与
# 实习中的菜狗时刻 #
278777次浏览 2739人参与
# 腾讯求职进展汇总 #
199471次浏览 1662人参与
# 阿里求职进展汇总 #
70849次浏览 761人参与
# 如果有时光机,你最想去到哪个年纪? #
24667次浏览 504人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
37462次浏览 343人参与
# 风评不好的公司,你会去吗? #
20702次浏览 94人参与
# 上班苦还是上学苦呢? #
90522次浏览 781人参与
# 大疆求职进展汇总 #
413499次浏览 2934人参与
# 国企还是互联网,你怎么选? #
90139次浏览 702人参与
# 远程面试的尴尬瞬间 #
20452次浏览 296人参与
# 软件开发2024笔面经 #
2325636次浏览 48220人参与
# 如何一边实习一边秋招 #
999864次浏览 12697人参与
# 如果中了500万,你会离职吗? #
13552次浏览 145人参与