zoukankan      html  css  js  c++  java
  • Atcoder 212F Greedy Takahashi

    Problem Statement

    There are N cities numbered 1 through N, and MM buses that go between these cities. The i-th bus (1≤i≤M) departs from City Ai at time Si+0.5 and arrive at City Bi at time Ti+0.5.

    Takahashi will travel between these N cities. When he is in City p at time t, he will do the following.

    1. If there is a bus that departs from City p not earlier than time t, take the bus that departs the earliest among those buses to get to another city.
    2. If there is no such bus, do nothing and stay in City p.

    Takahashi repeats doing the above until there is no bus to take. It is guaranteed that all M buses depart at different times, so the bus to take is always uniquely determined. Additionally, the time needed to change buses is negligible.

    Here is your task: process Q queries, the i-th (1≤i≤Q)of which is as follows.

    • If Takahashi begins his travel in City Yi at time Xi, in which city or on which bus will he be at time Zi?

    Constraints

    • 2≤N≤105
    • 1≤M≤105
    • 1≤Q≤105
    • 1≤Ai,Bi≤N (1≤i≤M)
    • Ai≠Bi (1≤i≤M)
    • 1≤Si<Ti≤109 (1≤i≤M)
    • Si≠Sj (i≠j)
    • 1≤Xi<Zi≤109 (1≤i≤Q)
    • 1≤Yi≤N (1≤i≤Q)
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    N M Q
    A1 B1 S1 T1
    A2 B2 S2 T2
    ⋮⋮
    AM BM SM TM
    X1 Y1 Z1
    X2 Y2 Z2
    ⋮⋮
    XQ YQ ZQ
    

    Output

    Print Q lines. The i-th line should contain the response to the i-th query as follows.

    • If Takashi is on some bus at time Zi, print two integers representing the city from which the bus departs and the city at which the bus arrives, in this order, with a space between them.
    • Otherwise, that is, if Takahashi is in some city at time Zi, print an integer representing that city.

    Sample Input 1 Copy

    3 2 3
    1 2 1 3
    2 3 3 5
    1 1 5
    2 2 3
    1 3 2
    

    Sample Output 1 Copy

    2 3
    2
    3
    

    In the first query, Takahashi will travel as follows.

    1. Start in City 1 at time 1.
    2. Take the bus that departs from City 1 at time 1.5 and arrives at City 2 at time 3.5
    3. Take the bus that departs from City 2 at time 3.5 and arrives at City 3 at time 5.5
    4. Since no bus departs from City 3 at time 5.5 or later, stay in City 3 (forever).

    At time 5, he will be on the bus that departs from City 2 and arrives at City 3. Thus, as specified in the Output section, we should print 2 and 3 with a space between them.


    Sample Input 2 Copy

    8 10 10
    4 3 329982133 872113932
    6 8 101082040 756263297
    4 7 515073851 793074419
    8 7 899017043 941751547
    5 7 295510441 597348810
    7 2 688716395 890599546
    6 1 414221915 748470452
    6 4 810915860 904512496
    3 1 497469654 973509612
    4 1 307142272 872178157
    374358788 4 509276232
    243448834 6 585993193
    156350864 4 682491610
    131643541 8 836902943
    152874385 6 495945159
    382276121 1 481368090
    552433623 2 884584430
    580376205 2 639442239
    108790644 7 879874292
    883275610 1 994982498
    

    Sample Output 2 Copy

    4
    6 1
    4 1
    8
    6 1
    1
    2
    2
    7 2
    1
    

    题目翻译

    (n) 个城市,(m) 辆车,对于第 (i) 辆车在 (s_i+0.5) 时刻从 (u_i) 出发,于 (t_i+0.5) 抵达 (v_i)

    在一个点时会选择还没出发且离当前出发时刻最近的车坐上,直到没有车。给出 (q) 个询问,问最后会到哪里。

    如果在车上,则输出上一个城市和车编号。在城市则输出城市号

    题目解析

    考虑询问数(Q<=10^5),猜测单次查询的复杂度为(O(logn))

    倍增,预处理(f[i][j])表示乘坐(i)号车,换乘(2^j)后所在的车

    使用STL的set的lower_bound,通过(T_i)求出(f[i][0])

    询问时枚举(j),一直换乘到无法换乘或时间已到

    通过结束时间(Z)与当前车次(T_{now})判断在车上还是城市

    总结一下此题的要点:

    然在每个点要走的都是和时间相关的,但对于一个时间之后,上车下车的走发是唯一的,所以可以把车作为状态。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<set>
    using namespace std;
    typedef long long ll;
    int n,m,q;
    int u[100005],v[100005],s[100005],t[100005];
    int f[100005][20];
    set<pair<int, int> >g[100005];
    int main(){
        int x,y,z;
        cin>>n>>m>>q;
        for (int i=1;i<=m;i++){
            scanf("%d%d%d%d",&u[i],&v[i],&s[i],&t[i]);
            g[u[i]].insert(make_pair(s[i],i));
        }
        for (int i=1;i<=m;i++){
            set<pair<int, int> >::iterator it;
            it=g[v[i]].lower_bound(make_pair(t[i],0));
            if (it!=g[v[i]].end()) 
                f[i][0]=it->second;
        }
        for (int i=1;i<=17;i++){
            for (int j=1;j<=m;j++){
                f[j][i]=f[f[j][i-1]][i-1];
            }
        }
        while (q--){
            scanf("%d%d%d",&x,&y,&z);
            set<pair<int, int> >::iterator it;
            it=g[y].lower_bound(make_pair(x,0));
            if (it==g[y].end()||z<=s[it->second]){
                printf("%d
    ",y);
            }else{
                int now=it->second;
                for (int i=17;i>=0;i--){
                    if (f[now][i]&&s[f[now][i]]<z) now=f[now][i];
                }
                if (t[now]<z) printf("%d
    ",v[now]);
                else printf("%d %d
    ",u[now],v[now]);
            }
        }
    }
    
  • 相关阅读:
    STM32 -- 硬件知识
    PCIe相关的操作命令
    [转载]PCI/PCIe基础——配置空间
    [转载]网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、VXLAN
    [转载]TSO、UFO、GSO、LRO、GRO和RSS介绍
    Linux应用函数 -- 字符串
    初级PLC
    中断方式下进行串口通讯的正确方法
    [altium] Altium Designer2013 13.3.4 (10.1881.28608) 完美版
    32个最热CPLD-FPGA论坛
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/15135111.html
Copyright © 2011-2022 走看看