当前位置: 首页 > news >正文

Concert Tickets 二分+并查集

题目描述

There are n concert tickets available, each with a certain price. Then, m customers arrive, one after another.
Each customer announces the maximum price they are willing to pay for a ticket, and after this, they will get a ticket with the nearest possible price such that it does not exceed the maximum price.

输入

The first input line contains integers n and m(1≤ n, m≤ 2*105): the number of tickets and the number of customers.
The next line contains n integers h1,h2,...,hn(1≤ hi≤ 109): the price of each ticket.
The last line contains m integers t1,t2,...,tm(1≤ ti≤ 109): the maximum price for each customer in the order they arrive.

输出

Print, for each customer, the price that they will pay for their ticket. After this, the ticket cannot be purchased again.
If a customer cannot get any ticket, print -1.

样例输入 Copy
5 3
5 3 7 8 5
4 8 3
样例输出 Copy
3
8
-1

二分查找,分配门票(每个人越贵越好)

用并查集查询已经被购买的情况(一直查询更小的票价,售空为止)

find需要特判-1的情况,不然会运行错误

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,t[200009],h[200009],c[200009];
int find(int x){if(x==-1)return -1;if(c[x]!=x)c[x]=find(c[x]);return c[x];
}
int main(){scanf("%d%d",&n,&m);for(int i=0;i<n;++i)scanf("%d",&h[i]);for(int i=0;i<n;++i)c[i]=i;for(int i=0;i<m;++i)scanf("%d",&t[i]);sort(h,h+n);for(int i=0;i<m;++i){int l=0,r=n-1,mid,p=-1;while(l<=r){mid=(l+r)>>1;if(h[mid]>t[i]){r=mid-1;}else{l=mid+1;p=mid;}}if(p==-1){cout<<-1<<'\n';continue;}int ans=find(c[p]);if(ans==-1){cout<<ans<<'\n';}else{cout<<h[ans]<<'\n';c[ans]=(ans==0)?-1:find(ans-1);}}return 0;
}

http://www.lryc.cn/news/598361.html

相关文章:

  • Visual Studio 2010-.Net Framework 4.0-DevExpress安装
  • 使用adb 发送广播 动态改变app内的值
  • Lua(文件I/O)
  • VB解除excel保护工作表
  • 【docker】将已有mysql脚本导入镜像内使用
  • API安全监测工具:数字经济的免疫哨兵
  • Linux服务器安全自动化审计实战:一键扫描账户/网络/进程/计划任务风险(附开源脚本)
  • 项目——在线五子棋对战
  • pyarmor加密源代码
  • 闲庭信步使用图像验证平台加速FPGA的开发:第三十三课——车牌识别的FPGA实现(5)车牌字符的识别
  • OpenCV —— contours_matrix_()_[]
  • 删除排序数组中的重复项
  • 微服务的编程测评系统6-管理员登录前端-前端路由优化
  • 一文说清楚Hive中常用的聚合函数[collect_list]
  • 亿级流量短剧平台架构演进:高并发场景下的微服务设计与性能调优
  • Matplotlib详细教程(基础介绍,参数调整,绘图教程)
  • IO密集型、CPU密集型、负载、负载均衡
  • 校园英语杂志《校园英语》杂志社校园英语编辑部2025年第15期目录
  • 考研初试专业分146!上岸新疆大学!信号与系统考研经验,通信考研小马哥。
  • GitHub Actions打包容器,推送 AWS ECR 并使 EKS 自动拉取以完成发版部署
  • Redis数据类型与内部编码
  • Webpack配置原理
  • MongoDB 和 Elasticsearch(ES)区别
  • Windows 下配置 GPU 用于深度学习(PyTorch)的完整流程
  • matrix-breakout-2-morpheus靶场通过
  • 基于深度学习的胸部 X 光图像肺炎分类系统(二)
  • 小架构step系列24:功能模块
  • Android中compileSdk,minSdk,targetSdk的含义和区别
  • M3295NL专为千兆以太网设计,支持100/1000Mbps全双工通信M3295支持4对5类UTP电缆
  • SparkSQL 子查询 IN/NOT IN 对 NULL 值的处理