大炮打蚊子。两个优先队列,存前后时间。如果后面有就取后面最小,没有后面就取前面最小。
实际上不需要优先队列
#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <string>
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef unsigned long long ULL;
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
priority_queue<int,vector<int>, greater<int>> q_back;
priority_queue<int,vector<int>, greater<int>> q_front;
int n, h, m;
cin >> n >> h >> m;
int ans = 0;
while (n--)
{
int a, b;
cin >> a >> b;
if(a * 60 + b >= h * 60 + m) q_back.push(a * 60 + b);
else q_front.push(a * 60 + b);
}
if(q_back.size())
{
ans = q_back.top();
ans = ans - (h * 60 + m);
}
else
{
ans = q_front.top();
ans = 24 * 60 - (h * 60 + m) + ans;
}
cout << ans / 60 << ' ' << ans % 60 << endl;
}
}