【2015-2016 XVI Open CupH】【STL大融合】Hierarchy 公司人来人往 公寓入住进出 求公司老大与寝室长

来源:互联网 发布:淘宝怎么看店铺红包5元 编辑:程序博客网 时间:2024/06/10 09:38
H. Hierarchy
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The hierarchical structure of the new office of Galactical Ministry of Bureacracy is very complicated. Office has 109 departments, numbered by sequential integers from 1 to 109.

Initially all departments are empty. Then two types of the events may happen:

  1. Arrival: new person is moved from old offices into some department. For this person are known his name and birthday. Assume this event is k-th arrival event for this department. Then person's local id is set to k.
  2. Departure: selected person in some department with local id k is moved out this department to the old offices.

The head of each department is defined by the following way:

  • If some person is strictly older, than than all other people who are currently working in this department, he became the head of the department.
  • In case of tie (two or more oldest employees), the one with least local id in this department became the head of the department.

The head of office is defined by the following way:

  • If some person who currently works in new office is strictly older, than all other people who currently are working in the new office, this person is the head of the office.
  • In case of tie person, who is working in the department with least number, have priority.
  • In case of tie person with least local id on this department have priority.

You are asked to write a program which after each event prints name of the head of office and head of department, which is related to this event.

Input

First line of the input contains one integer n — number of events (1 ≤ n ≤ 105). Then n events follow.

First line of the event description contains one integer t — type of event (|t| = 1).

If t = 1, then new person is moved to the new office. Then second line contains one integer D — number of department, where new person is coming (1 ≤ D ≤ 109), next line contains non-empty string composed of no more than 10 lowercase English letters — name of the person, and next line contains his date of birth in Unified Decimal Galaxy Time format dd: mm: yyyy, where dd is for day, mm — for month and yyyy — for year, 00 ≤ dd, mm ≤ 99, 0001 ≤ yyyy ≤ 9999. You may assume that all names in the input file are pairwise distinct.

If t =  - 1, then some person is moved back to the old offices. In this case second line contains two integers D and k — number of department and local id of person who is moved (1 ≤ D ≤ 109). It is guaranteed that k does not exceed total number of people who were moved to this department at the moment of event and that all k in the requests with t =  - 1 are pairwise different.

Output

After each event print two space-separated strings: name of the head of the new office and name of head of department, which is related to this event. If new office or department contains no employees, printf "Vacant" instead.

Example
input
8110rab01:01:000111000000000tor02:01:0001-110 1110tur01:01:0001-110 2-11000000000 115bor99:99:999915rot99:99:9999
output
rab rabrab tortor Vacanttur turtor VacantVacant Vacantbor borbor bor
Note

Lets check what happened in the sample.

First event: in department 10 arrived new employee rab. He got local id 1 in this department, became the head of department and head of office.

Second event: in office 1000000000 arrived new employee tor. He got local id 1 in this department, became the head of department, but because rab's birthday is 01: 01: 0001, and tor's only 02: 01: 0001, tor is younger and does not became the head of office.

Third event: from department 10 removed employee with local id 1, i.e. rab. Department is empty, so we are printing "Vacant". In other departments only tor is working, so he became the head of office.

Fourth event: in department 10 arrived new employee tur. He got local id 2 in this department, became the head of department (because department was empty) and became the new head of the office, because his birthday is 01: 01: 0001, so he is older, thantor.

Fifth event: from department 10 removed employee with local id 2, i.e. tur. Department is empty, so we are printing "Vacant". In other departments only tor is working, so he became new head of office.

Sixth event: from department 1000000000 removed employee with local id 1, i.e. tor. Department is empty, so we are printing "Vacant". Office is empty, so we are printing "Vacant".

Seventh event: in department 5 arrived new employee bor. He got local id 1 in this department, became the head of department and head of office. Eight event: in department 5 arrived new employee rot. He got local id 2 in this department. His birthday is the as thebor's, but his local id is greater, so bor keeps his position as head of the department. Similarly, rot have same birthday as bor, they are working in same department, but his local id is greater, so bor keeps his position as head of the office.

#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>#include <unordered_map>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;struct Person{int birthday;int depart_id;int local_id;string name;}p[N]; int Pid;//决定公司Boss的所有人排序规则struct cmp{bool operator() (const int& a, const int& b)const{if (p[a].birthday != p[b].birthday)return p[a].birthday < p[b].birthday;if (p[a].depart_id != p[b].depart_id)return p[a].depart_id <p[b].depart_id;return p[a].local_id < p[b].local_id;}};set<int,cmp>all;//把department的大id离散化为小idunordered_map<int, int>depord; int Did;struct Depart{set<int,cmp>peo;//对这个公寓的人进行排序vector<int>p;//记录这个人是这个公司的第几个}dep[N];int n;int main(){while (~scanf("%d", &n)){int Pid = 0;//人的编号int Did = 0;//公寓的编号for (int i = 1; i <= n; ++i){int t; scanf("%d", &t);if (t == 1){int d, x; scanf("%d", &d);char s[15]; scanf("%s", s); p[++Pid].name = s;//cin >> p[++Pid].name;p[Pid].birthday = 0;scanf("%d", &x); p[Pid].birthday += x;scanf(":%d", &x); p[Pid].birthday += x * 100;scanf(":%d", &x); p[Pid].birthday += x * 10000;//找到这个人所在的公寓编号dif (depord.find(d) == depord.end())depord[d] = ++Did; d = depord[d];//修改这个人的信息并把这个人插入公寓,同时插入全体dep[d].p.push_back(Pid);p[Pid].depart_id = d;p[Pid].local_id = dep[d].p.size();dep[d].peo.insert(Pid);all.insert(Pid);x = *all.begin(); puts(p[x].name.c_str());x = *dep[d].peo.begin(); puts(p[x].name.c_str());}else{int d, x; scanf("%d%d", &d, &x);d = depord[d];x = dep[d].p[x - 1];dep[d].peo.erase(x);all.erase(x);if (all.empty())printf("Vacant ");else { x = *all.begin(); puts(p[x].name.c_str()); }if (dep[d].peo.empty())puts("Vacant");else { x = *dep[d].peo.begin(); puts(p[x].name.c_str()); }}}}return 0;}/*【trick&&吐槽】1,读题很重要。做题的时候一定要把所有题目都读一遍,否则很有可能遇到虽然会做却并没有AC的遗憾。2,puts(string.c_str())会更快一些3,一个我傻逼的地方是,虽然公司整体和公寓内部的排序规则不一样,但其实只用整体的排序规则就可以应付自如了。4,#include <bits/stdc++.h> c++11的遍历有一种写法会方便很多,for(auto it : 结构体){blabla}同时unordered_map也更快【题意】公司有1e9个公寓,编号从1到1e9。每个公寓有很多房间,但初始时都是空的。有n个事件发生,事件有两种类型,通过t告诉你。t==1,一个人(姓名,生日)来到我们公司入职,会告诉你他来到的公寓号D,如果是这个公寓号第k个来到的,就入这个公寓的k号房间。t==-1,一个人,从他的D号公寓k号房间,离开了这个公司公寓中年龄最大的人作为公寓长如果两个人年龄相同,房间编号最小的那个作为公寓长。公司中也有一个老大。1,如果一个人年龄最大,作为老大。2,年龄相同,公寓编号小的优先3,公寓相同,房间编号小的优先要你在每次操作后,输出谁是公式老大的名字,输出与这个人相关的公寓的公寓长的名字。【类型】STL 模拟【分析】这道题是需要STL技巧的题目。做法很多================做法一:我的做法=================1,我存每个人的信息:年龄,公寓编号,房间编号,名字,存在一个N的struct中。2,我们有一个全局的排序set,通过编号,按照3关键规则对所有人做排序,决定公司老大3,我们有一个把大的department的id映射为小department id的map4,我存每个公寓的信息,来的所有人的先后顺序,以及内部排序的set如果有人来了,我更新人的信息,加入all和depart如果有人走了,我查询到这个人是谁,然后删除信息这样这道题就可以无压力AC啦。【时间复杂度&&优化】O(nlogn)*/


0 0
原创粉丝点击