题解:P13455 [GCJ 2008 Qualification] Train Timetable

· · 题解

思路

首先分别记录下每个时间(单位为分钟,下同)两个车站新增加的列车数量,接着从小到大遍历每个时间,算出这个时间两个车站的总列车数量分别是多少,因为车站的总列车数量不能为负,所以当某个车站的总列车数量小于 0 时,该车站需准备的列车数量就要加上负的该车站的总列车数量,这样该车站的总列车数量就变成 0 了,遍历完后,按格式输出答案即可。

代码

#include <bits/stdc++.h>
#define ll long long
//#define rw() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifndef rw()
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

namespace Pv {
    template<typename T> inline void r(T &in) {
        in = 0;
        bool bo = 0;
        char ch = getchar();
        while (!isdigit(ch))
            bo ^= (ch == '-'), ch = getchar();
        while (isdigit(ch))
            in = (in << 1) + (in << 3) + (ch ^ 48), ch = getchar();
        if (bo)
            in = -in;
    }
    template<typename T> inline void w(T out) {
        static char op[25];
        int top = 0;
        if (out < 0) {
            putchar('-');
            do {
                op[++top] = -(out % 10) + 48, out /= 10;
            } while (out);
        } else {
            do {
                op[++top] = out % 10 + 48, out /= 10;
            } while (out);
        }
        while (top)
            putchar(op[top--]);
    }
    template<typename T, typename... Ts> inline void r(T &in, Ts &... ins) {
        r(in), r(ins...);
    }
    template<typename T, typename... Ts> inline void w(T out, Ts... outs) {
        w(out), w(outs...);
    }
    inline void w(const char *p) {
        while (*p) {
            putchar(*p++);
        }
    }
}
#undef getchar
#undef putchar
using namespace Pv;
#endif
using namespace std;
ll T,n,m,k,ans,ans1,a[26*60],b[26*60];
int main() {
    r(T);
    for (ll t=1;t<=T;t++){
        r(k,n,m);
        ans=ans1=0;
        set<ll>s;
        memset(a,0,sizeof a);
        memset(b,0,sizeof b);
        while(n--){
            ll x,y;
            r(x,y);
            a[x*60+y]--;
            s.insert(x*60+y);
            r(x,y);
            b[x*60+y+k]++;
            s.insert(x*60+y+k);
        }
        while(m--){
            ll x,y;
            r(x,y);
            s.insert(x*60+y);
            b[x*60+y]--;
            r(x,y);
            s.insert(x*60+y+k);
            a[x*60+y+k]++;
        }
        ll suma=0,sumb=0;
        for (auto i:s){
            suma+=a[i];
            sumb+=b[i];
            if (suma<0){
                ans-=suma;
                suma=0;
            }
            if (sumb<0){
                ans1-=sumb;
                sumb=0;
            }
        }
        w("Case #"),w(t),w(": "),w(ans),w(" "),w(ans1),w("\n");
    }
    return 0;
}