当前位置: 代码迷 >> 综合 >> Codeforces 1379 D. New Passenger Trams(双指针)
  详细解决方案

Codeforces 1379 D. New Passenger Trams(双指针)

热度:61   发布时间:2024-01-31 01:10:28.0

There are many freight trains departing from Kirnes planet every day. One day on that planet consists of ? hours, and each hour consists of ? minutes, where ? is an even number. Currently, there are ? freight trains, and they depart every day at the same time: ?-th train departs at ?? hours and ?? minutes.

The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at 0 hours and ? minutes, where 0≤?<?2, the second tram departs ?2 minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.

To allow passengers to board the tram safely, the tram must arrive ? minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at 0 hours and ? minutes, where ?<?, then the freight trains can not depart during the last ??? minutes of the day.

A schematic picture of the correct way to run passenger trams. Here ?=2 (therefore, the number of passenger trams is 2?=4), the number of freight trains is ?=6. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length ? before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments.
Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of ? to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.

Input
The first line of input contains four integers ?, ?, ?, ? (1≤?≤100000, 1≤?≤109, 2≤?≤109, ? is even, 1≤?≤?2) — the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.

? lines follow, each contains two integers ?? and ?? (0≤??<?, 0≤??<?) — the time when ?-th freight train departs. It is guaranteed that no freight trains depart at the same time.

Output
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time ?. Second line of output should contain freight trains that need to be canceled.

Examples
inputCopy
2 24 60 15
16 0
17 15
outputCopy
0 0

inputCopy
2 24 60 16
16 0
17 15
outputCopy
1 0
2
Note
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.

In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if ?∈[1,15], then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If ?=0 or ?∈[16,29], then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose ?=0. Another possible option is to cancel the first train and choose ?=13.

题意:
一个星球一天有h小时,每小时有m分钟。有n辆货车,出发时间告诉你了。
有客车,每半个小时一趟,而且每趟出发的前k分钟载人,都不能有货车。
要求你安排客车的出发时间,使得能出发的货车最多,同时输出不能出发的货车。

思路:
因为每半小时一趟,所以可以直接以 m 2 \frac{m}{2} 为模数进行求解,将所有货车的出发时间放在 m o d ?? m 2 \mod\frac{m}{2} 下,
然后你枚举出发的时间,
问题就成了一个循环序列,找一个长度为 k k 的子区间覆盖最多的点。
不过注意本题是段区间,所以你维护了 [ t ? k , t ] [t-k,t] 的区间,对应 t ? k t-k 为开始载人开始,可以走货车,对应 t t 为载人结束,也可以走货车。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <iostream>using namespace std;typedef long long ll;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 9;int main() {int n,h,m,k;scanf("%d%d%d%d",&n,&h,&m,&k);vector<pair<int,int> >vec;m /= 2;for(int i = 1;i <= n;i++) {int x,y;scanf("%d%d",&x,&y); //h,mvec.push_back({y % m,i});vec.push_back({y % m + m,i});}sort(vec.begin(),vec.end());pair<int,int> ans = {1e9,-1};int j = 0;for(int i = 0;i < 2 * n;i++) {while(vec[i].first - vec[j].first >= k) {j++;}if(i >= n) {ans = min(ans,{i - j,vec[i].first});}}printf("%d %d\n",ans.first,ans.second % m);for(int i = 0;i < 2 * n;i++) {int l = ans.second - k,r = ans.second;if(vec[i].first < r && vec[i].first > l) {printf("%d ",vec[i].second);}}printf("\n");return 0;
}