Starting a Scenic Railroad Service(前缀和+差分)
Posted acerkoo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Starting a Scenic Railroad Service(前缀和+差分)相关的知识,希望对你有一定的参考价值。
Starting a Scenic Railroad Service
时间限制: 2 Sec 内存限制: 128 MB提交: 59 解决: 21
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Jim, working for a railroad company, is responsible for planning a new tourist train service. He is sure that the train route along a scenic valley will arise a big boom, but not quite sure how big the boom will be.
A market survey was ordered and Jim has just received an estimated list of passengers’ travel sections. based on the list, he’d like to estimate the minimum number of train seats that meets the demand.
Providing as many seats as all of the passengers may cost unreasonably high. Assigning the same seat to more than one passenger without overlapping travel sections may lead to a great cost cutback.
Two different policies are considered on seat assignments. As the views from the train windows depend on the seat positions, it would be better if passengers can choose a seat. One possible policy (named ‘policy-1’) is to allow the passengers to choose an arbitrary seat among all the remaining seats when they make their reservations. As the order of reservations is unknown, all the possible orders must be considered on counting the required number of seats.
The other policy (named ‘policy-2’) does not allow the passengers to choose their seats; the seat assignments are decided by the railroad operator, not by the passengers, after all the reservations are completed. This policy may reduce the number of the required seats considerably.
Your task is to let Jim know how different these two policies are by providing him a program that computes the numbers of seats required under the two seat reservation policies.
Let us consider a case where there are four stations, S1, S2, S3, and S4, and four expected passengers p1, p2, p3, and p4 with the travel list below.
The travel sections of p1 and p2 do not overlap, that of p3 overlaps those of p1 and p2, and that of p4 does not overlap those of any others.
Let’s check if two seats would suffice under the policy-1. If p1 books a seat first, either of the two seats can be chosen. If p2 books second, as the travel section does not overlap that of p1,the same seat can be booked, but the other seat may look more attractive to p2. If p2 reserves a seat different from that of p1, there will remain no available seats for p3 between S1 and S3
(figure I.1).
With three seats, p3 can find a seat with any seat reservation combinations by p1 and p2. p4 can also book a seat for there are no other passengers between S3 and S4 (figure I.2).
For this travel list, only three seats suffice considering all the possible reservation orders and seat preferences under the policy-1.
On the other hand, deciding the seat assignments after all the reservations are completed enables a tight assignment with only two seats under the policy-2 (figure I.3).
A market survey was ordered and Jim has just received an estimated list of passengers’ travel sections. based on the list, he’d like to estimate the minimum number of train seats that meets the demand.
Providing as many seats as all of the passengers may cost unreasonably high. Assigning the same seat to more than one passenger without overlapping travel sections may lead to a great cost cutback.
Two different policies are considered on seat assignments. As the views from the train windows depend on the seat positions, it would be better if passengers can choose a seat. One possible policy (named ‘policy-1’) is to allow the passengers to choose an arbitrary seat among all the remaining seats when they make their reservations. As the order of reservations is unknown, all the possible orders must be considered on counting the required number of seats.
The other policy (named ‘policy-2’) does not allow the passengers to choose their seats; the seat assignments are decided by the railroad operator, not by the passengers, after all the reservations are completed. This policy may reduce the number of the required seats considerably.
Your task is to let Jim know how different these two policies are by providing him a program that computes the numbers of seats required under the two seat reservation policies.
Let us consider a case where there are four stations, S1, S2, S3, and S4, and four expected passengers p1, p2, p3, and p4 with the travel list below.
The travel sections of p1 and p2 do not overlap, that of p3 overlaps those of p1 and p2, and that of p4 does not overlap those of any others.
Let’s check if two seats would suffice under the policy-1. If p1 books a seat first, either of the two seats can be chosen. If p2 books second, as the travel section does not overlap that of p1,the same seat can be booked, but the other seat may look more attractive to p2. If p2 reserves a seat different from that of p1, there will remain no available seats for p3 between S1 and S3
(figure I.1).
With three seats, p3 can find a seat with any seat reservation combinations by p1 and p2. p4 can also book a seat for there are no other passengers between S3 and S4 (figure I.2).
For this travel list, only three seats suffice considering all the possible reservation orders and seat preferences under the policy-1.
On the other hand, deciding the seat assignments after all the reservations are completed enables a tight assignment with only two seats under the policy-2 (figure I.3).
输入
The input consists of a single test case of the following format.
n
a1 b1
.
.
.
an bn
Here, the first line has an integer n, the number of the passengers in the estimated list of passengers’ travel sections (1 ≤ n ≤ 200 000). The stations are numbered starting from 1 in their order along the route. Each of the following n lines describes the travel for each passenger by two integers, the boarding and the alighting station numbers, ai and bi, respectively (1 ≤ ai < bi ≤ 100 000). Note that more than one passenger in the list may have the same boarding and alighting stations.
n
a1 b1
.
.
.
an bn
Here, the first line has an integer n, the number of the passengers in the estimated list of passengers’ travel sections (1 ≤ n ≤ 200 000). The stations are numbered starting from 1 in their order along the route. Each of the following n lines describes the travel for each passenger by two integers, the boarding and the alighting station numbers, ai and bi, respectively (1 ≤ ai < bi ≤ 100 000). Note that more than one passenger in the list may have the same boarding and alighting stations.
输出
Two integers s1 and s2 should be output in a line in this order, separated by a space. s1 and s2 are the numbers of seats required under the policy-1 and -2, respectively.
样例输入
4
1 3
1 3
3 6
3 6
样例输出
2 2
思路:
易知,第一个答案即为最大相交区间个数,第二个即为差分过程中的最大值。
对上车、下车分别求前缀和,同时对上车时刻、下车时刻差分约束一下。
代码如下:
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; const int maxm=2e6+10; int up[maxn],down[maxn],d[maxn]; int x[maxm],y[maxm]; int n,m=0,ans1=0,ans2=0; int main(){ scanf("%d",&n); for (int i=1; i<=n; i++){ scanf("%d%d",&x[i],&y[i]),m=max(m,y[i]); up[x[i]]++,down[y[i]]++;d[x[i]]++,d[y[i]]--; } for (int i=1; i<maxn; i++) up[i]+=up[i-1],down[i]+=down[i-1],d[i]+=d[i-1]; for (int i=1; i<=n; i++) { ans1=max(ans1,up[y[i]-1]-down[x[i]]); ans2=max(ans2,d[x[i]]); } printf("%d %d ",ans1,ans2); return 0; }
以上是关于Starting a Scenic Railroad Service(前缀和+差分)的主要内容,如果未能解决你的问题,请参考以下文章
Starting nagios:This account is currently not available.