EOJ has recently opened a new live channel, so that coders can broadcast their coding online and chat with the audience at the same time. It has become children’s favorite entertainment at night, well, other than the monthly contest.
It may sound hard to believe, but the EOJ team has recently encountered some trouble in terms of the live channel statistics. Basically they want to know two things:
- The maximum number of children watching the live channel simultaneously;
- The average number of children online, during the show, which starts at the beginning of second 1and ends at the end of second m.
Input
The first line contains two space-separated integers n and m (1≤n≤250 000, 1≤m≤109), denoting the number of children log-in record, the length of the show.
The next n line each contains two space-separated integers si and ti (1≤si≤ti≤m), meaning that a child starts to watch the channel at the start of second si, and leave the channel at the end of second ti. Notice that the same pair of (s,t) might appear multiple times, they are believed to be different records.
You can safely assume that these records all belong to different children.
Output
In the first line, output the maximum number. You can write it as an integer, or a floating number, as you like.
In the second line, output the average number as a floating number.
The absolute or relative error should be not greater than 10−12.
Examples
input
2 10
1 5
5 10
output
2
1.1
input
4 10
1 3
4 4
8 8
5 7
output
1
8E-1
input
5 10
5 6
4 7
3 8
2 9
1 10
output
5.000000000000000
3
求区间覆盖最大值,扫描线的模板。
平均值,就是所有区间孩子的总数,除区间总长度吧
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn =3e5+100;
struct node
{
double id,w;
}G[maxn*3];
bool cmp(node a,node b)
{
if(a.id<b.id)return 1;
else if(a.id==b.id)
{
if(a.w>b.w)return 1;
return 0;
}
return 0;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int ans=0;
double u,v;
double sum_1=0;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&u,&v);
sum_1+=v-u+1;
G[ans].id=u;
G[ans++].w=1;
G[ans].id=v;
G[ans++].w=-1;
}
sort(G,G+ans,cmp);
ll sum=0;
ll max_1=0;
for(int i=0;i<ans;i++)
{
sum+=G[i].w;
max_1=max(max_1,sum);
}
printf("%lld\n%.12lf\n",max_1,sum_1/(double)m);
return 0;
}
本文介绍了一个针对直播频道观众数量统计的问题解决方法,包括如何计算同时在线的最大观众数及整个直播期间的平均观众数。通过使用扫描线算法进行区间覆盖最大值的计算,并给出了一段实现该功能的C++代码。
3万+

被折叠的 条评论
为什么被折叠?



