多队列模拟. 与POJ #1025 Department类似, 不过简化很多. 貌似这类模拟题经常出现. 用STL中的优先队列 (priority_queue<>) 很好写.


这题我写得很不顺, 老年选手退步太快, 记录一下我犯的一个很隐蔽的错误, 从前对此毫无认识, 想想都可怕, 太菜了.

这道题优先队列里维护的事件 (event) 是等待.

释放这些等待的先后顺序如下:

在同一队列 (此"队列"指某个office门前的队列, 与"优先队列"中的"队列"含义不同;下同) 中的两个事件, 按(开始等待时刻, 学号)这两个关键字排序. 在不同队列中的两事件按开始等待的时刻排序.

思路是比较容易想到的. 但我的实现里有个很subtle的错误.

Wrong implementation

#include <bits/stdc++.h>
using namespace std; const int N{1<<14}, M{1<<7};
int s[N], res[N];
vector<pair<int,int>> r[N]; // registration
int avail[M];
int n, m, k; struct W{
int id, beg, idx;
int des()const{
return r[id][idx].first;
}
int dur()const{
return r[id][idx].second;
}
int num()const{
return s[id];
}
int end()const{
return max(beg, avail[des()])+dur(); // error-prone
}
W next(){
return {id, end()+k, idx+1};
}
void out(){
cout<<id<<' '<<beg<<' '<<idx<<' '<<avail[des()]<<endl;
}
}; // greater<_Tp> is a function object
// constexpr std::greater<_Tp>::operator()(const _Tp &, const_Tp &)const bool cmp(const W &a, const W &b){ //! > must be a const
if(a.des()!=b.des()){
return a.beg>b.beg;
}
else{
return a.beg!=b.beg?a.beg>b.beg:a.num()>b.num();
}
} priority_queue<W, vector<W>, decltype(cmp)*> que(cmp); int main(){
cin>>n>>m>>k;
for(int i=0; i<n; i++){
int q, t;
cin>>s[i]>>t>>q;
for(; q--; ){
int o, t;
cin>>o>>t;
r[i].push_back({o, t});
}
que.push({i, t+k, 0});
} for(; !que.empty(); ){
W top=que.top();
que.pop();
if(top.idx<r[top.id].size()-1){
que.push(top.next());
}
else{
res[top.id]=top.end();
}
avail[top.des()]=top.end();
}
for(int i=0; i<n; i++)
cout<<res[i]<<endl;
return 0;
}

注意其中的比较函数cmp:

bool cmp(const W &a, const W &b){  //! > must be a const
if(a.des()!=b.des()){
return a.beg>b.beg;
}
else{
return a.beg!=b.beg?a.beg>b.beg:a.num()>b.num();
}
}

首先必须认识到: 优先队列要求元素的键值 (key) 在所定义的比较函数下是全序的 (totally ordered).

If $X$ is totally ordered under $\le$, then the following statements hold for all $a, b$ and $c$ in $X$:

if $a \le b$ and $b \le a$ then $ a=b$ (antisymmetry);

if $a \le b$ and $b\le c$ then $a \le c$ (transitivity);

$a \le b$ or $b \le a$ (totality).

我所定义的比较函数并不是全序的. 问题中的集合$X$内的元素是一个三元组$(id, des, time)$. 我们定义的比较函数cmp是一个strict weak ordering (<). 这个binary relation应当满足

if $a < b$ and $b<c$, then $a<c$

$a< b$ or $ b<a$ or $a=b$.

但是据此会导出矛盾, 考虑

$a =(id_1, des_1, t), b=(id_2, des_1, t), c=(id_3, des_2, t), id_1 < id_2 \Longrightarrow a > b, a=c, b=c$.

矛盾!

因而这个比较函数不是全序的.

但是可以略微改一下:

以$time$为第一关键字, $des$为第二关键字, $id$为第三关键字.

这样不但思路更清楚, 代码也可以简化

Implementation

#include <bits/stdc++.h>
using namespace std; const int N{1<<14}, M{1<<7};
int s[N], res[N];
vector<pair<int,int>> r[N]; // registration
int avail[M];
int n, m, k; struct W{
int id, beg, idx;
int des()const{
return r[id][idx].first;
}
int dur()const{
return r[id][idx].second;
}
auto make_tuple()const{
return std::make_tuple(beg, des(), num());
}
int num()const{
return s[id];
}
bool operator<(const W &rhs)const{
return make_tuple() > rhs.make_tuple();
}
int end()const{
return max(beg, avail[des()])+dur(); // error-prone
}
W next(){
return {id, end()+k, idx+1};
}
void out(){
cout<<id<<' '<<beg<<' '<<idx<<' '<<avail[des()]<<endl;
}
}; priority_queue<W> que; int main(){
cin>>n>>m>>k;
for(int i=0; i<n; i++){
int q, t;
cin>>s[i]>>t>>q;
for(; q--; ){
int o, t;
cin>>o>>t;
r[i].push_back({o, t});
}
que.push({i, t+k, 0});
} for(; !que.empty(); ){
W top=que.top();
que.pop();
if(top.idx<r[top.id].size()-1){
que.push(top.next());
}
else{
res[top.id]=top.end();
}
avail[top.des()]=top.end();
}
for(int i=0; i<n; i++)
cout<<res[i]<<endl;
return 0;
}

hihoCoder 1401 Registration的更多相关文章

  1. struts2启动报错:ERROR com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor - Conversion registration error

    [framework] 2019-12-05 11:34:05,441 - org.springframework.web.context.ContextLoader -5352 [RMI TCP C ...

  2. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  3. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  4. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  5. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  6. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  7. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  8. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

  9. 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词   代码注意点: 1. getline(istre ...

随机推荐

  1. 【福吧资源网整理】老男孩-python运维6期 不加密

    老男孩-python运维6期 不加密,连夜整理出来分享给大家老男孩的python教程确实不错. 教程目录: 下载地址:http://www.fu83.cn/thread-204-1-1.html  

  2. Java的性能优化

    http://www.toutiao.com/i6368345864624144897/?tt_from=mobile_qq&utm_campaign=client_share&app ...

  3. Android -- 自定义权限

    在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序,系统或者用户带来负面影响的操作.如果应用需要执行某些操作,就需要声明使用这个操作对应的权限. (在manifest ...

  4. FineUI(专业版)高清大图赏析!(第二波)

    FineUI(专业版)是由三生石上全新打造的基于 jQuery 的专业 ASP.NET 控件库,计划在七月下旬正式发布. 选择FineUI(专业版)的四大理由:1. 简单:专业版和开源版兼容(v4.x ...

  5. 深入理解计算机系统(1.2)---hello world的程序是如何运行的

    在写本章的内容之前,LZ先做个小广告.其实也不算是什么广告,就是LZ为了和各位猿友交流方便,另外也确实有个别猿友留言或者在博客里发短消息给LZ要联系方式.因此LZ斗胆建立了一个有关<深入理解计算 ...

  6. Linux epoll 笔记(高并发事件处理机制)

    wiki: Epoll优点: Epoll工作流程: Epoll实现机制: epollevent; Epoll源码分析: Epoll接口: epoll_create; epoll_ctl; epoll_ ...

  7. 重拾Blog

    上个月是我入职现在的公司三周年的月份,所以又续订了五年的合同,最近有一些思考,也不知道这个五年能否还会一直在这个公司工作. 一切随缘吧. 闲适有毒,忙碌的时光总是过的很快,自从加入这个公司以来,日常的 ...

  8. [BZOJ1579][Usaco2009 Feb]Revamping Trails 道路升级(二维最短路问题)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1579 分析: 设d[i][j]表示从1走到i.改了j条边的最短路径长度 如果设i相连的 ...

  9. MATLAB实现频数直方图——hist的使用

      "hist" is short for "Histogram(直方图.柱状图)". 1.N = hist(Y) bins the elements of Y ...

  10. ThreadLocal类学习笔记

    这个类在java1.2中就出现了,线程独有的变量(每个线程都有一份变量),使用它的好处之一就是可以少传许多参数. 在哪里用到它呢?有连接池的地方就有它的身影,连接池包括数据库连接池,网络连接池等. i ...