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

输入

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.

输出

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\n",ans1,ans2);
    return 0;
}

原文地址:https://www.cnblogs.com/acerkoo/p/9557254.html

时间: 2024-08-29 18:59:33

Starting a Scenic Railroad Service(前缀和+差分)的相关文章

Codeforces 479E Riding in a Lift:前缀和/差分优化dp

题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初始在a层,然后要玩k次电梯. 这栋楼里还有一个神秘实验室,在b层. 这让他每次坐电梯受到了限制: 当前在x层,然后要坐到y层,则必须满足|x-y|<|x-b| 问你共有多少种坐电梯的方案. 题解: 表示状态: dp[i][j] = numbers 表示当前在第i层,已经坐了j次电梯,此时的方案数.

HDU-6514 Monitor(二维前缀和+差分)

http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m. But recently Xiaoteng found that his crops were often stolen by a group of people,

前缀和&amp;差分

一:差分数组概念 一.差分数组的定义及用途 1.定义:对于已知有n个元素的数列d,建立记录它每项与前一项差值的差分数组f:显然,f[1]=d[1]-0=d[1];对于整数i∈[2,n],我们让f[i]=d[i]-d[i-1].//f[i]数组为差分数组,d[i]数组为原数组 2.简单性质:(1)计算数列各项的值:观察d[2]=f[1]+f[2]=d[1]+d[2]-d[1]=d[2]可知,d[i]=f[i]的前缀和.(2)计算数列每一项的前缀和:第i项的前缀和即为数列前i项的和,那么推导可知 /

二维前缀和差分+离散化

/* 二维前缀和求法 a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]; 构建前缀和 int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){//初始化 for(int j=1;j<=m;j++){ int x; scanf("%d",&x); a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]+x; } } 输入nm 以

cf之 前缀和差分

给定一个n×n的WB矩阵,给定一个k∗k的能把B变成W的橡皮擦,求橡皮擦作用一次后,全为W的行.列总数最大值 连接:http://codeforces.com/contest/1200/problem/D #include<bits/stdc++.h> using namespace std; int A[2004][2004]; char C[2004][2004]; int B[2004][2004]; int E[2004][2004]; int F[2004][2004]; int n

2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest

地址 Rank Solved A B C D E F G H I J K 16/160 27/130 O O O . O O ? . O . ? O: 当场通过 ?: 赛后通过 .: 尚未通过 A Secret of Chocolate Poles solved by chelly chelly's solution B Parallel Lines solved by ch ch's solution C Medical Checkup solved by chelly chelly's so

gym 101986

A - Secret of Chocolate Poles 队友写的. 好像水水的. //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optimize(4) //#pragma GCC optimize("unroll-loops") //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("

前缀和和差分模板(AcWing 795-798)

前缀和分一维前缀和和二维前缀和,前缀和可以帮我们快速统计一段范围内的合. 需要简单的理解 一维前缀和 —— 模板题 AcWing 795. 前缀和S[i] = a[1] + a[2] + ... a[i]:a[l] + ... + a[r] = S[r] - S[l - 1]: 二维前缀和 —— 模板题 AcWing 796. 子矩阵的和S[i, j] = 第i行j列格子左上部分所有元素的和以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵的和为:S[x2, y2] - S[x1 -

Android中Service的一个Demo例子

Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Service做过多讲解.  代码是从网上找的一个例子,Copy下来发现代码不完全正确,稍微修改了下.  AndroidManifest.xml <application android:icon="@drawable/ic_launcher" android:label="@stri