模拟题。

这题第一个障碍是现在少见的循环电梯 ('pater-noster' elevator)

"The building has `pater-noster' elevator, i.e. elevator build up from several cabins running all around."

这种叫做 paster-noster 的电梯是 running all around 的,如动画所示,题目中费解的地方便清楚了。(欲知详情,请往维基百科)

第二个难点:模拟

模拟的核心是排队——等电梯或等房间

一开始可能感觉无从下手,模拟题的分析方法最主要的是分析事件。

准确地说,这道题有且仅有两个事件

排队等候(wating in queue)

行动(progress)

题目要求输出的是特工的活动记录(record)

solution:

1.用结构体表示事件:排队等候E,行动S

2.将电梯也看做房间编号是XX00

3.用优先队列维护排队等候的序列priority_queue<E, vector<E> > que,为此还需定义小于(<)运算符

bool operator < (const E& a,  const E& b){ ...}

4.维护vector<S> record[26]:特工的行动序列

5.房间状态的维护

(1)room[11][11] :房间空闲的起始时刻,初始化为0,模拟过程中要不断更新

(2)updated[11][11]: updated[i][j]表示room[i][j]是否更新过

若当前等待事件的目标房间(或电梯)更新过,则要将当前等待事件再次入队

6.处理过程中时间统一化成秒,输出时再转化成指定的格式

7.其他细节见代码

网上见到的题解大多代码过长,可读性差,我自己写了个比较简明的版本,200行多一点

#include<bits/stdc++.h>
using namespace std; typedef pair<int,int> P; int room[][]; //room[i][j]:房间空闲的起始时刻
bool updated[][]; //起始时刻是否已更新 vector<P> agent[]; int time[], sta[]; int done[];//已经访问的房间数目 struct S //行程
{
int des;
int cost;
S(int des, int cost):des(des), cost(cost){}
}; struct E //排队
{
int code;
int beg;
int pos;
E(int code, int beg, int pos):code(code), beg(beg), pos(pos){};
}; bool operator <(const E &a, const E &b)
{
//两人不是站在同一队列中等待
if(a.pos!=b.pos) return a.beg > b.beg;
//两人同时到达
if(a.beg==b.beg) return a.code > b.code; int f=a.pos/, r=a.pos%; //等电梯
if(a.pos%==)
{
int t1=(a.beg%?(a.beg/+)*:a.beg);
int t2=(b.beg%?(b.beg/+)*:b.beg);
//两人乘同一班电梯,资历高的先进
if((t1<=room[f][r]&&t2<=room[f][r])||t1==t2)
return a.code>b.code;
return t1>t2;
}
else //等房间
{
//两人都得等,资历高的先进
if(a.beg<=room[f][r]&&b.beg<=room[f][r]) return a.code>b.code; //!room[f][r]更新会影响这种比较
//至少有一个人不用等,先到的先进
else return a.beg > b.beg;
}
} priority_queue<E, vector<E> >que; //排队
vector<S> record[]; //行程 void input()
{
FILE* fp=stdin; //提交时改成stdin
char c;
int h, m, s, pos, dur;
while(fscanf(fp," %[A-Z] ",&c))
{
int idx=c-'A';
fscanf(fp,"%d:%d:%d",&h,&m,&s);
time[idx]=sta[idx]=*h+*m+s; while(fscanf(fp,"%d",&pos),pos)
{
fscanf(fp,"%d",&dur);
agent[idx].push_back(P(pos, dur));
}
agent[idx].push_back(P(pos, )); //! Exit
}
} void init()
{
for(int i=; i<; i++)
{
if(!agent[i].size()) continue;
if(agent[i][].first/==)
record[i].push_back(S(agent[i][].first,));//往一层某房间
else record[i].push_back(S(,));//往一层电梯
time[i]+=;
que.push(E(i,time[i],record[i].back().des));
}
} void simulator()
{
int wait, id, cost, to, f, r;
while(!que.empty())
{
E e=que.top();
que.pop();
id=e.code;
f=e.pos/;
r=e.pos%;
if(updated[f][r])
{
que.push(e);
updated[f][r]=false;
continue;
} if(r==) //等电梯
{
if(e.beg<=room[f][r]) wait=room[f][r]-e.beg;
else wait=(e.beg%? -e.beg%: );
if(wait) record[id].push_back(S(e.pos, wait));
time[id]+=wait;
room[f][r]=time[id]+;
updated[f][r]=true;
to=agent[id][done[id]].first;
if(to==) //!Exit
{
cost=*(e.pos/-);
record[id].push_back(S(, cost));
record[id].push_back(S(to,));
continue;
}
cost=*(max(to/,e.pos/)-min(to/,e.pos/));
record[id].push_back(S(to/*, cost)); //在电梯里
time[id]+=cost;
record[id].push_back(S(to, )); //往房间
time[id]+=;
que.push(E(id,time[id],to));
}
else //等房间
{
wait=max(room[f][r]-e.beg,);
if(wait) record[id].push_back(S(e.pos,wait));
time[id]+=wait;
cost=agent[id][done[id]].second;
record[id].push_back(S(-e.pos, cost)); //取反
time[id]+=cost; room[f][r]=time[id];
updated[f][r]=true; done[id]++;
to=agent[id][done[id]].first;
if(to==&&f==) //!Exit
{
record[id].push_back(S(to, ));
continue;
}
if(to/!=f) to=f*; //需等电梯
record[id].push_back(S(to, )); //往电梯
time[id]+=;
que.push(E(id,time[id],to));
}
}
} void t_p(int t)
{
int h=t/, m=(t%)/, s=t%%;
printf("%02d:%02d:%02d ",h,m,s);
} void e_p(int pre, int cur)
{
if(pre==) {printf("Entry\n"); return;}
if(cur==) {printf("Exit\n"); return;}
if(cur<) {printf("Stay in room %04d\n",-cur); return;}
if(pre==cur)
{
if(cur%==) printf("Waiting in elevator queue\n");
else printf("Waiting in front of room %04d\n",cur);
return;
}
if(pre%==&&cur%==) {printf("Stay in elevator\n"); return;};
if(pre<)
{
if(cur%==) printf("Transfer from room %04d to elevator\n",-pre);
else printf("Transfer from room %04d to room %04d\n",-pre, cur);
return;
}
else printf("Transfer from elevator to room %04d\n",cur);
} void output()
{
int pre_pos, cur_pos, t;
for(int i=; i<; i++)
{
if(!record[i].size()) continue;
t=sta[i];
pre_pos=; printf("%c\n",'A'+i);
for(int j=; j!=record[i].size(); j++)
{
t_p(t);
t_p(t+=record[i][j].cost);
cur_pos=record[i][j].des;
e_p(pre_pos, cur_pos);
pre_pos=cur_pos;
}
printf("\n");
}
} int main()
{
input();
init();
simulator();
output();
return ;
}

POJ #1025 Department的更多相关文章

  1. POJ #2448 A New Operating System

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 1165   Accepted: 110 Case Time Limit: ...

  2. hihoCoder 1401 Registration

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

  3. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  4. POJ 1065 Wooden Sticks(zoj 1025) 最长单调子序列

    POJ :http://poj.org/problem?id=1065 ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId= ...

  5. POJ 1065 & ZOJ 1025

    #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> ...

  6. poj 1266 Cover an Arc.

    http://poj.org/problem?id=1266 Cover an Arc. Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  7. [划分树] POJ 2104 K-th Number

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 51732   Accepted: 17722 Ca ...

  8. 【POJ】2104 K-th Number(区间k大+主席树)

    http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...

  9. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

随机推荐

  1. 菜鸟的数据库实战-4-数据阅读器SqlDataReader

    老铁们大家好啊,我是菜鸟思奎,今天我学习的是数据库和前端的连接用到的字符串,如果有什么纰漏希望大家在评论区指正.阿里嘎多. 我的环境是Visual Studio 2008 + Microsoft SQ ...

  2. UVA 11214 Guarding the Chessboard 守卫棋盘(迭代加深+剪枝)

    暴力,和八皇后很像,用表示i+j和i-j标记主对角线,但是还是要加一些的剪枝的. 1.最裸的暴搜 6.420s,差点超时 2.之前位置放过的就没必要在放了,每次从上一次放的位置开始放 0.400s # ...

  3. Object Modeling

    https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/Objec ...

  4. ValidForm验证表单

    在做项目时,要求熟悉项目中验证表单的插件,所以学习一下validForm这个插件 http://validform.rjboy.cn/document.html#validformObject

  5. 03_2_JAVA中的面向对象与内存解析

    03_2_JAVA中的面向对象与内存解析 1. 成员变量 成员变量可以是Java语言中任何一种数据类型(包括基本数据类型和引用数据类型) 在定义成员变量时可以对其初始化,如果不对其初始化,Java使用 ...

  6. ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题

    除之前的Spring相关包,还有structs2包外,还需要Hibernate的相关包 首先,Spring整合其他持久化层框架的JAR包 spring-orm-4.2.4.RELEASE.jar  ( ...

  7. UI Testing in Xcode 7

    参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...

  8. 协议(Protocol)与委托代理(Delegate)

    协议(Protocol)的作用: 1. 规范接口,用来定义一套公用的接口: 2. 约束或筛选对象. 代理(Delegate): 它本身是一种设计模式,委托一个对象<遵守协议>去做某件事情, ...

  9. 【数学 技巧】divisor

    没考虑重复lcm处理被卡TLE没A真是可惜 题目大意 $n$为$k-可表达的$当且仅当数$n$能被表示成$n$的$k$个因子之和,其中$k$个因子允许相等. 求$[A,B]$之间$k-可表达$的数的个 ...

  10. MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”

    或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...