首页 > 其他分享 >t2

t2

时间:2023-11-17 22:33:37浏览次数:23  
标签:return log int res t2 public userId

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* 本题答题框内的代码仅为待实现代码,执行或提交代码时,仅包含待实现部分(含所需的import)
* CodeCheck/Cmetrics工具也仅扫描待实现部分。
* 若需要完整框架用于本地调试,请点击答题框上面的“下载完整框架代码”进行下载。
* jdk基线版本为jdk 11, 平台支持jdk 17;使用完整框架代码需满足jdk基线要求。另将于24年引入fastjson 1.2.83
* VideoService([8, 1, 1], [10, 15, 30])
* allocateChannel(3, 107, 1)
* allocateChannel(3, 108, 1)
* allocateChannel(5, 110, 1)
* queryChannel(108)
* freeChannel(13, 108)
*
* @author 命题组
* @since 2023-1-1
*/
class VideoService {
public static void main(String[] args) {
VideoService vs = new VideoService(new int[]{8, 1, 1}, new int[]{10, 15, 30});
System.out.println(vs.allocateChannel(3, 107, 1));
System.out.println(vs.allocateChannel(3, 108, 1));
System.out.println(vs.allocateChannel(5, 110, 1));
System.out.println(vs.queryChannel(108));
System.out.println(vs.freeChannel(13, 108));
}

/**
* 使用记录
*/
private Map<Integer, UseVideoLog> userLogMap = new ConcurrentHashMap<>();

/**
* 资源池
*/
private Map<String, VideoResource> resPoolMap = new ConcurrentHashMap<>();
VideoService(int[] channels, int[] charge) {
// 注册资源通道池
for (int i = 0; i < channels.length; i++) {
int num = channels[i];
int price = charge[i];
for (int j = 0; j < num; j++) {
VideoResource res = new VideoResource();
res.setResId(UUID.randomUUID().toString());
res.setPrice(price);
res.setVideoType(i);
res.setStatus(true);
resPoolMap.put(res.getResId(), res);
}
}
}

boolean allocateChannel(int time, int userId, int videoType) {
// 筛选资源
List<VideoResource> fitterResList = resPoolMap.values().stream()
.filter(c -> c.videoType >= videoType && c.status).sorted(Comparator.comparingInt(o -> o.videoType))
.collect(Collectors.toList());
VideoResource res;
if (fitterResList == null || fitterResList.size() == 0) {
// 锁定资源失败
return false;
} else {
// 成功锁定
res = fitterResList.get(0);
}

List<VideoResource> fitterResList1 = resPoolMap.values().stream().filter(c -> c.videoType == videoType)
.sorted(Comparator.comparingInt(o -> o.price)).collect(Collectors.toList());
// 占用通道记录日志
UseVideoLog log = new UseVideoLog();
log.setUserId(userId);
log.setTime(time);
log.setActualVideoType(res.getVideoType());
log.setReqVideoType(videoType);
log.setPrice(fitterResList1.get(0).getPrice());
log.setResId(res.getResId());
userLogMap.put(userId, log);
// 资源锁定
res.setStatus(false);
resPoolMap.put(res.getResId(), res);
return true;
}

int freeChannel(int time, int userId) {
UseVideoLog log = userLogMap.get(userId);
if (log == null) {
return -1;
}
// 释放资源
VideoResource res = resPoolMap.get(log.getResId());
res.setStatus(true);
resPoolMap.put(log.getResId(), res);
// 移除使用记录
userLogMap.remove(userId);
// 分配给其他通道升级的用户 (按规则)
List<UseVideoLog> useVideoLogDownUserList = userLogMap.values().stream()
.filter(c -> c.getActualVideoType() > c.getReqVideoType() && c.getReqVideoType() <= res.videoType)
.sorted(Comparator.comparingInt(o -> o.userId)).collect(Collectors.toList());
// 降级匹配的用户
if (useVideoLogDownUserList != null && useVideoLogDownUserList.size() > 0) {
UseVideoLog downUser = useVideoLogDownUserList.get(0);
freeChannel(time, downUser.getUserId());
}
return (time - log.getTime()) * log.getPrice();
}

int queryChannel(int userId) {
UseVideoLog log = userLogMap.get(userId);
if (log == null) {
return -1;
}
return log.getActualVideoType();
}
}

class UseVideoLog {
String resId;
int userId;

int time;

int reqVideoType;

int actualVideoType;

int price;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public int getTime() {
return time;
}

public void setTime(int time) {
this.time = time;
}

public int getReqVideoType() {
return reqVideoType;
}

public void setReqVideoType(int reqVideoType) {
this.reqVideoType = reqVideoType;
}

public int getActualVideoType() {
return actualVideoType;
}

public void setActualVideoType(int actualVideoType) {
this.actualVideoType = actualVideoType;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public String getResId() {
return resId;
}

public void setResId(String resId) {
this.resId = resId;
}
}

class VideoResource {
String resId;
int videoType;

boolean status;

int price;

public int getVideoType() {
return videoType;
}

public void setVideoType(int videoType) {
this.videoType = videoType;
}

public boolean isStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public String getResId() {
return resId;
}

public void setResId(String resId) {
this.resId = resId;
}
}

标签:return,log,int,res,t2,public,userId
From: https://www.cnblogs.com/cytc/p/17839811.html

相关文章

  • 2023最新!Git2
    2023最新!Git2.40.0于win10环境下的安装git官网地址:https://git-scm.com/download/win/导航目录2023最新!Git2.40.0于win10环境下的安装导航一、下载Git二、安装Git三、检验一、下载GitGit官网选择自己所需的版本下载二、安装Git双击安装程序,并选择next推荐更换路径(避免文......
  • add方法在return的适时候就形成了一个闭包,包含n=4399这个值,这个n不是result和result2
    在浏览器控制台中执行以下代码,输出的结果是functiontest(){varn=4399;functionadd(){n++;console.log(n);}return{n:n,add:add}}varresult=test();varresult2=test();result.add();result.add();console.log(result.n)......
  • 【pwn】[HNCTF 2022 WEEK2]ret2libc --rop构造泄露libc
    这道题是简单的libc,不过多分析了exp:frompwnimport*fromLibcSearcherimport*io=remote("node5.anna.nssctf.cn",28341)elf=ELF("./pwn")put_got=elf.got["puts"]put_plt=elf.plt["puts"]main_addr=0x4011A6rdi=0x401273  #用RO......
  • 【GJOI 2023.11.13 T2】 字符串匹配
    字符串匹配题意:给出两个字符串\(a,b\),求:\[\sum_{1\lel\ler\len}\sum_{l\lei\lej\ler}(a[l...r]回文)(a[i...j]==b)\times(r-l+1)mod2\]其中\(n,m\le10^6\)。解题思路首先,因为\(a[l..r]\)长度为奇数,它又要回文,所以它一定是要有一个回文中心的。那我......
  • SpringBoot2和SpringBoot3有什么区别
    SpringBoot2和SpringBoot3有什么区别1.最低环境的区别Java版本:SpringBoot2的最低版本要求为Java8,支持Java9;而SpringBoot3决定使用Java17作为最低版本,并支持Java19。SpringFramework版本:SpringBoot2基于SpringFramework5开发;而SpringBoot3构建基于SpringFramework6之上。......
  • XPT2046
    XPT2046是一种典型的逐次逼近型模数转换器(SARADC),包含了采样/保持、模数转换、串口数据输出等功能。(采样:将一个时间上连续变化的模拟量转化为时间上离散的变化量。 保持:将采样结果存储起来,知道下次采样。 数模转换包含量化和编码。量化:将采样电平归化与之接近的离散数字电平......
  • NOIP模拟赛35T1T2
    T1KAMEN只能说一言难尽。60pt暴力模拟每一个石头往下掉的情况。在这里,我并没有打暴力,而是用set存储了每一列的X和O的石子分布情况。当前节点的位置在(x,y),寻找x列中比y大的第一个位置在ny(这里可以用upper_bound),那么石子在这一列能往下掉到的位置就是(x,ny-1)然后再判断能......
  • 洛谷 NOIP 2023 模拟赛 T2 汪了个汪
    洛谷NOIP2023模拟赛T2汪了个汪考试建出正解图不知道怎么处理,题解区樱雪喵博客薄纱。樱雪喵题解链接Ps:笔者语文爆炸,不建议阅读本文思路首先你会发现,一共有\(\frac{n(n-1)}{2}\)个二元组,有\(\frac{n(n-1)}{2}\)个横向相邻数对。按照题目要求,一个横向数对对应一个二......
  • T2
    题目描述给你下列7种形状,问恰好填满\(n*2\)的方格有多少种方案(每种形状可任意旋转)后三种形状纯粹是出题人的恶意,d用没有做法一:暴力不会做法二:递推定义:f[i]为填满\(i*2\)的方格的方案数g[i]为填满\(i*2\)的方格不能被腰斩的方案数解释:例如当\(n=4\)时......
  • train.cs.nctu.edu.tw: ret2libc
    来源本题来自ctfwiki中ret2libc后的一道练习题检查保护只开启了NX保护ida查看跟前面的shellcode的课后练习类似,泄露了/bin/sh地址和puts函数的地址gdb调试断点下在main,结合ida中v4=esp+1ch得到偏移为1chexpfrompwnimport*fromLibcSearcherimport......