Aizu - 1386 - Starting a Scenic Railroad Service(思路)

版权声明:沃斯里德小浩浩啊 https://blog.csdn.net/Healer66/article/details/82463991

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 dierent 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 p1p1, p2p2, p3p3, and p4p4 with the travel list below.

passenger from to
p1p1 S1 S2
p2p2 S2 S3
p3p3 S1 S3
p4p4 S3 S4

The travel sections of p1p1 and p2p2 do not overlap, that of p3p3 overlaps those of p1p1 and p2p2, and that of p4p4 does not overlap those of any others.

Let's check if two seats would suffice under the policy-1. If p1p1 books a seat first, either of the two seats can be chosen. If p2p2 books second, as the travel section does not overlap that of p1p1, the same seat can be booked, but the other seat may look more attractive to p2p2. If p2p2 reserves a seat different from that of p1p1, there will remain no available seats for p3p3 between S1 and S3 (Figure I.1).

Figure I.1. With two seats

With three seats, p3p3 can find a seat with any seat reservation combinations by p1p1and p2p2. p4p4 can also book a seat for there are no other passengers between S3 and S4 (Figure I.2).

Figure I.2. With three seats

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).

Figure I.3. Tight assignment to two seats

Input

The input consists of a single test case of the following format.

nn
a1a1 b1b1
...
anan bnbn

Here, the first line has an integer nn, the number of the passengers in the estimated list of passengers' travel sections (1≤n≤2000001≤n≤200000). The stations are numbered starting from 1 in their order along the route. Each of the following nn lines describes the travel for each passenger by two integers, the boarding and the alighting station numbers, aiai and bibi, respectively (1≤ai<bi≤1000001≤ai<bi≤100000). Note that more than one passenger in the list may have the same boarding and alighting stations.

Output

Two integers s1s1 and s2s2 should be output in a line in this order, separated by a space. s1s1 and s2s2 are the numbers of seats required under the policy-1 and -2, respectively.

Sample Input 1

4
1 3
1 3
3 6
3 6

Sample Output 1

2 2

Sample Input 2

4
1 2
2 3
1 3
3 4

Sample Output 2

3 2

Sample Input 3

10
84 302
275 327
364 538
26 364
29 386
545 955
715 965
404 415
903 942
150 402

Sample Output 3

6 5

题意:

n个人,ai上车,bi下车,两种预定方案:1:随便订,但是要选之前没订购过的。2:最优策略,选完位置再安排座位。

问两种方案需要的座位各多少。

思路:

对于第一种,答案是人数最多的区间的人数,因为只要区间有重叠,选票可能会冲突,把最大的冲突给安排了不就行了吗。

对于第二种,只要知道最多同时有多少人在车上就行了。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int ans1,ans2,a[maxn],b[maxn],r[maxn],MAX;
int sc[maxn];
int xc[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i]>>b[i];
        sc[a[i]]++;//a[i]上车人数
        xc[b[i]]++;//b[i]下车人数
        r[a[i]]++;
        r[b[i]]--;//站点[的人数
        MAX = max(MAX, b[i]);//最远站
    }
    for(int i = 1;i<= MAX;i++)
    {
        sc[i]+= sc[i-1];
        xc[i]+= xc[i-1];
    }
    for(int i=1; i<=n;i++)
        ans1=max(ans1,sc[b[i]-1]-xc[a[i]]);//精华所在啊,求的是在该区间的总人数
    for(int i=1;i<=MAX;i++)
    {
        r[i]+=r[i-1];
        ans2 = max(ans2,r[i]);
    }
    cout<<ans1<<' '<<ans2<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Healer66/article/details/82463991
今日推荐