2017微软秋季校园招聘在线编程笔试(三)

题目3 : Registration Day

  • 时间限制:10000ms
  • 单点时限:1000ms
  • 内存限制:256MB

描述
It's H University's Registration Day for new students. There are M offices in H University, numbered from 1 to M. Students need to visit some of them in a certain order to finish their registration procedures. The offices are in different places. So it takes K units of time to move from one office to another.
There is only one teacher in each office. It takes him/her some time to finish one student's procedure. For different students this time may vary. At the same time the teacher can only serve one student so some students may need to wait outside until the teacher is available. Students who arrived at the office earlier will be served earlier. If multiple students arrived at the same time they will be served in ascending order by student number.
N new students need to finish his/her registration. They are numbered from 1 to N. The ith student's student number is Si. He will be arrived at H University's gate at time Ti. He needs to visit Pi offices in sequence which are Oi,1, Oi,2, ... Oi,Pi. It takes him Wi,1, Wi,2, ... Wi,Pi units of time to finish the procedure in respective offices. It also takes him K units of time to move from the gate to the first office.
For each student can you tell when his registration will be finished?
输入
The first line contains 3 integers, N, M and K. (1 <= N <= 10000, 1 <= M <= 100, 1 <= K <= 1000)
The following N lines each describe a student.
For each line the first three integers are Si, Ti and Pi. Then following Pi pairs of integers: Oi,1, Wi,1, Oi,2, Wi,2
, ... Oi,Pi, Wi,Pi.
(1 <= Si <= 2000000000, 1 <= Ti <= 10000, 1 <= Pi <= M, 1 <= Oi,j <= M, 1 <= Wi,j <= 1000)
输出
For each student output the time when he finished the registration.
样例提示
Student 1600012345 will be arrived at the gate at time 500. He needs to first visit Office #1 and then Office #2. He will be arrived at office #1 at time 600. He will leave office #1 at time 1100. Then He will arrive at office #2 at 1200. At the same time another student arrives at the same office too. His student number is smaller so he will be served first. He leaves Office #2 at 1700. End of registration.

Student 1600054321 will be arrived at the gate at time 1100. He will be arrived at Office #2 at 1200. Another student with smaller student number will be served first so he waits for his turn until 1700. He leaves Office #2 at 2000. End of registration.
样例输入
2 2 100
1600012345 500 2 1 500 2 500
1600054321 1100 1 2 300
样例输出
1700
2000
参考答案1:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;
#define fst first
#define snd second

const int MAXN = 100010;
const ll inf = 1000000000000LL;

vector<pii> ow[10010];
//priority_queue<pii, vector<pii>, greater<pii>> pq[110];
ll e[10010];
int lst[10010];

int main(void) {

    map<int, int> sid;
    int n, m, K;
    cin >> n >> m >> K;
    //for (int i = 1; i <= m; ++ i) e[i].fst = inf;
    vector<ll> s(n), t(n), p(n);
    for (int i = 0; i < n; ++ i) {
        lst[i] = 0;
        cin >> s[i] >> t[i] >> p[i];
        sid[s[i]] = i;
        e[i] = t[i] + K;
        for (int j = 0; j < p[i]; ++ j) {
            ll o, w;
            cin >> o >> w;
            ow[i].emplace_back(pii(o, w));
            /*
            if (j == 0 && (t[i] + K < e[o].fst || (t[i] + K == e[o].fst && e[o].snd > s[i]))) {
                e[o].fst = t[i] + K;
                e[o].snd = s[i];
            }
            */
        }
    }

    priority_queue<pii, vector<pii>, greater<pii>> pq;
    for (int i = 0; i < n; ++ i) pq.push(pii(e[i], s[i]));

    vector<ll> curt(m + 1, 0);
    vector<ll> ans(n);
    while ( !pq.empty() ) {
        ll ee = inf, eid = 0;
        /*
        for (int i = 0; i < n; ++ i) if (lst[i] < p[i]) {
            if (e[i] < ee || (e[i] == ee && s[eid] > s[i])) {
                eid = i;
                ee = e[i];
            }
        }
        */
        /*
        for (int i = 1; i <= m; ++ i) {
            if (!pq[i].empty() && pq[i].top().fst < ee) {
                ee = pq[i].top().fst;
                eid = i;
            }
        }
        */
        ee = pq.top().fst, eid = sid[pq.top().snd];
        pq.pop();
        //if (ee == inf) break;

        int id = ow[eid][lst[eid]].fst;
        //pii cur = pq[eid].top(); pq[eid].pop();
        //ll time = cur.fst;
        //ll id = sid[cur.snd];
        //cerr << eid << ' ' << id << ' ' << e[eid] << endl;
        if (lst[eid] >= (ll)ow[eid].size() - 1) {
            ans[eid] = max(e[eid], curt[id]) + ow[eid][lst[eid]].snd;
            e[eid] = ans[eid];
            curt[id] = ans[eid];
        } else {
            //ll nid = ow[eid][lst[eid] + 1].fst;
            ll ntime = max(e[eid], curt[id]) + ow[eid][lst[eid]].snd + K;
            curt[id] = ntime - K;
            e[eid] = ntime;
            pq.push(pii(ntime, s[eid]));
        }
        ++ lst[eid];
    }

    for (int i = 0; i < n; ++ i) cout << ans[i] << endl;

    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容