POJ1025 Department
模拟题。
这题第一个障碍是现在少见的循环电梯 ('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 ;
}
POJ1025 Department的更多相关文章
- [LeetCode] Department Top Three Salaries 系里前三高薪水
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- [LeetCode] Department Highest Salary 系里最高薪水
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- university, school, college, department, institute的区别
这些个词没有太大区别,有时候有些词是可以通用的,而有些用法则是随着地域时间的不同而变迁. 一般说来,college译作“学院”,它是university (综合性大学)的一个组成部分,例如,一所综合大 ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用.题目描述依然拷贝.简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来. Intr ...
- org.hibernate.PropertyNotFoundException: Could not find a getter for employee in class com.itcast.f_hbm_oneToMany.Department
<hibernate-mapping package="com.itcast.f_hbm_oneToMany"> <class name="Depart ...
- LeetCode - 185. Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries
SQL 架构 Create table If Not Exists Employee (Id ), Salary int, DepartmentId int) Create table If Not ...
随机推荐
- 工作随笔——一次简单的Maven加速构建实战
注意:所有的编译.打包.部署全部是通过Jenkins完成的. 公司内部有一个项目,开始做的时候已经预计到会有很多客服端.所以开发就搞了如下的结构: fft-api # 公用的API,所有的程序都必须使 ...
- Theano2.1.16-基础知识之调试:常见的问题解答
来自:http://deeplearning.net/software/theano/tutorial/shape_info.html Debugging Theano: FAQ and Troubl ...
- unity3d 扩展NGUI Tweener —— TweenTime
这是今天做的一个小功能 策划想要一个时间滚动效果 那就搞呗!思路和之前写的tweenFillAmount一样 传送门:http://www.cnblogs.com/shenggege/p/479892 ...
- MVVM小记
这篇小记源自于codeproject上的一篇文章 http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explai ...
- 高仿Windows Phone QQ登录界面
给 TextBox文本框前添加图片 扩展PhoneTextBox:添加一个类"ExtentPhoneTextBox"继承 PhoneTextBox ,在"ExtentPh ...
- 遇到技嘉 Gigabyte UEFI DualBIOS问题
我的板子是技嘉H87-D3H二手板子,用了差不多三年左右,一直感觉不错,承担着高强度的开发任务. 问题描述: 无法开机,按下电源后,主板通电状态灯亮,CPU和显卡风扇转动,显示器显示下图: 2至3秒后 ...
- 2016 5.03开始记录我的it学习。
好多谢立成师兄给我这个网址,我发现博客园不仅仅可以随笔记载很多东西,还是一个资源丰富的网站,接下来的四年我会用心去记录这些学习的点滴.
- mac 设置阿里企业邮箱
接收邮件服务器:pop3.mxhichina.com或pop3.您的域名,端口:110 发送邮件服务器:smtp.mxhichina.com或smtp.您的域名,端口:25 IMAP协议设置 接收邮件 ...
- 1018MYSQL数据迁移到SQLSERVER
-- 第一步利用MYSQL将数据结果的脚本迁移出来-- 第二步利用POWERDESGINER的反向功能,将脚本生成为物理模型 FILE-REVERSE DATEBASE -- 第三步将物理模型生成SQ ...
- URL参数GB2312和UTF-8编码 自动识别
网上找的,以备后用. 直接上代码: public static string QueryStringDecode(string key) { HttpRequest Request = System. ...