题目链接:https://acm.ecnu.edu.cn/contest/103/problem/C/
题目:一个电视节目播放时间段里,最多有几个人同时观看电视(这里只讲第一个问题,第二个问题就不说了)
思路:
一开始想用dp来解决这个最多问题,但是数据范围太大,没办法开数组;这道题挺考思维的;其实可以模拟一下,比如播放时间1有人看的话就+1,如果有人退的话就-1,然后继续看时间2有没有人进来看,如果有继续+1,如果有人退又-1,我们只要在枚举每一个播放时间,记录收看率的最高峰就行了;
第一次WA,是因为精度,题目要求12,我直接用了cout,卡了一段时间;
第二次WA,一开始用map来记录每一个时间段进来和退出去的人,然后数据太大,超内存了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
typedef long long ll;
const int Maxn = 5e6;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
struct Node {
ll t,o; // t是时间,o是操作,1或者-1,表示进来和退出
} a[Maxn];
bool cmp (const Node &a1, const Node &a2) {
if(a1.t == a2.t) return a1.o > a2.o; // 同一个时间,要先记录进来看的,然后才处理退出去的
else return a1.t < a2.t;
}
int main (void)
{
int n;
ll r,l,m,M;
double sum;
scanf("%d%lld",&n,&m);
sum = 0; M = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld",&l,&r);
a[++M].t = l;
a[M].o = 1;
a[++M].t = r;
a[M].o = -1;
sum+=(r-l+1);
}
sort(a+1,a+M+1,cmp);
ll tmp = 0,maxn = 0;
for (int i = 1; i <= M; ++i) {
tmp+=a[i].o;
maxn = max(maxn, tmp);
}
printf("%lld\n",maxn);
printf("%.14f\n",sum*(1.0)/m); //这里一定要注意精度 0.12f的题目要求的
return 0;
}