当前位置:网站首页>2022杭电杯超级联赛(5)
2022杭电杯超级联赛(5)
2022-08-05 10:29:00 【right_135】
1012 Buy Figurines
题解:需要两个优先队列,一个优先队列存储 正在购物的人的结束时间和所属队伍编号,另一个优先队列存储队伍信息(队伍编号,人数,最晚结束时间,以及队伍信息编号 信息编号越大表示队伍信息越新,老的队伍信息不可以用,碰到直接删除,保证使用的队伍信息必须是该队伍最新的信息),每个人来的时候,需要先看第一个优先队列 中有没有人要走了,走的话需要更新对应队伍的信息,并将新的队伍信息放到第二个优先队列中,然后再看队伍信息,找到第一个最新的队伍信息,将该人排到该队列后面即可。然后看时间最久的一个人的结束时间即可。
通过优先队列可以快速找到最符合条件的队伍,进而减少时间复杂度。
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;
struct student {
//人
int a,s;//a 到达时间 s 购物时间
int time;//结束时间
int num;//该人在哪只队伍
bool operator<(const student &a)const {
return time>a.time;
}
};
student stu[1000006];//保存人的信息
int sum[1000005];//保存各个队伍当前的总人数
bool cmp(student &a,student &b) {
//按照人到达的时间排序
return a.a<b.a;
}
struct quq {
//队伍信息
int num,idx;//队伍编号 队伍idx
int sum;//队伍人数
bool operator<(const quq &a)const {
if(sum!=a.sum)
return sum>a.sum;//如果人不相等 则人少的排在前面
if(num!=a.num) {
return num>a.num;//如果人数相等 编号小的在前面
}
return idx<a.idx;//如果编号也相等 则表示同一只队伍 则最新的队伍信息--idx大的 在前面
}
};
priority_queue<quq>q1;//队伍信息的 优先队列
priority_queue<student>st;//正在购物的人员信息的优先队列
ll js[1000006];//记录各个队伍当前最后一名的结束时间
int id[1000005];//记录队伍的最新idx
void solve() {
int n,m;
while(q1.size())q1.pop();
while(st.size())st.pop();
memset(js,0,sizeof(js));
memset(id,0,sizeof(id));
memset(sum,0,sizeof(sum));
memset(stu,0,sizeof(stu));
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++) {
scanf("%d%d",&stu[i].a,&stu[i].s);
}
for(int i=0; i<m; i++) {
quq qu;//初始化m只队伍信息
qu.num=i;//队伍编号
qu.idx=0;//队伍idx
qu.sum=0;//队伍没有人
q1.push(qu);
}
sort(stu,stu+n,cmp);//按照人物到达时间排序 从小到大
ll ma=0;
for(int i=0; i<n; i++) {
ll t=stu[i].a;//当前人的到达时间
while(st.size()) {
student s1=st.top();//正在购物 人的信息
if(s1.time<=t) {
// 购物时间到达 则该人应该走了 它所对应的队伍信息需要更新
st.pop();
sum[s1.num]--;//对应队伍的人数需要-1
quq temp;
temp.sum=sum[s1.num];
temp.idx=++id[s1.num];//队伍的idx需要+1
temp.num=s1.num;//队伍的编号
q1.push(temp);
} else {
break;
}
}
while(q1.size()) {
quq temp=q1.top();
if(temp.idx!=id[temp.num]) {
//如果该队伍已经不是该队伍的最新信息了..
q1.pop();
} else {
q1.pop();
temp.idx=++id[temp.num];//队伍idx+1;
t=max(t,js[temp.num]);//看人员的真实开始时间
t+=stu[i].s;
stu[i].time=t;//人员的结束时间
stu[i].num=temp.num;//人员所在的队伍编号
st.push(stu[i]); //进入购物
ma=max(ma,t);
js[temp.num]=t;//队伍的结束时间更新
sum[temp.num]++;//队伍的人数+1
temp.sum=sum[temp.num];
q1.push(temp);
break;
}
}
}
cout<<ma<<'\n';
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
solve();
}
}
边栏推荐
- 企业的数字化转型到底是否可以买来?
- How to choose coins and determine the corresponding strategy research
- High-quality DeFi application building guide to help developers enjoy DeFi Summer
- Import Excel/CSV from Sub Grid within Dynamics 365
- [Android]如何使用RecycleView in Kotlin project
- Jenkins manual (2) - software configuration
- RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
- [Strong Net Cup 2022] WP-UM
- 第五章:多线程通信—wait和notify
- 如何修改管理工具client_encoding
猜你喜欢
Opencv算术操作
单片机:温度控制DS18B20
反射修改jsessionid实现Session共享
three objects are arranged in a spherical shape around the circumference
高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer
[强网杯2022]WP-UM
Login function and logout function (St. Regis Takeaway)
深入理解 Istio 流量管理的超时时间设置
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口
随机推荐
基于MindSpore高效完成图像分割,实现Dice!
[Android] How to use RecycleView in Kotlin project
How does the official account operate and maintain?Public account operation and maintenance professional team
GPU-CUDA-图形渲染分析
FPGA: Basic Getting Started Button Controlling LED Lights
Microcontroller: temperature control DS18B20
Introduction to SD NAND Flash!
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
2022华数杯数学建模A题环形振荡器的优化设计思路思路代码分享
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
秘乐短视频挖矿系统开发详情
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
Voice-based social software development - making the most of its value
七夕来袭!还要做CDH数据迁移怎么办?来看看DistCp
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
nyoj754 黑心医生 结构体优先队列
【 temperature warning program DE development 】 event driven model instance
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing