求助大佬看看怎么改才能没有mle和re,谢谢啦
查看原帖
求助大佬看看怎么改才能没有mle和re,谢谢啦
1441755
LongEating楼主2024/10/18 01:13

[USACO18JAN] Lifeguards B

题面翻译

给定 nn 个左闭右开区间,问在去掉任意一个区间的情况下,区间的覆盖长度最大是多少。

题目描述

Farmer John has opened a swimming pool for his cows, figuring it will help them relax and produce more milk.

To ensure safety, he hires NN cows as lifeguards, each of which has a shift that covers some contiguous interval of time during the day. For simplicity, the pool is open from time t=0t=0 until time t=1000t=1000 on a daily basis, so each shift can be described by two integers, giving the time at which a cow starts and ends her shift. For example, a lifeguard starting at time t=4t=4 and ending at time t=7t=7 covers three units of time (note that the endpoints are "points" in time).

Unfortunately, Farmer John hired 1 more lifeguard than he has the funds to support. Given that he must fire exactly one lifeguard, what is the maximum amount of time that can still be covered by the shifts of the remaining lifeguards? An interval of time is covered if at least one lifeguard is present.

输入格式

The first line of input contains N(1N100)N (1\le N\le 100). Each of the next N lines describes a lifeguard in terms of two integers in the range 010000\dots 1000, giving the starting and ending point of a lifeguard's shift. All such endpoints are distinct. Shifts of different lifeguards might overlap.

输出格式

Please write a single number, giving the maximum amount of time that can still be covered if Farmer John fires 1 lifeguard.

样例 #1

样例输入 #1

3
5 9
1 4
3 7

样例输出 #1

7

代码

#include <stdio.h> int main(void){ //确定区间个数 int n; scanf("%d",&n); //输入多个区间并标记被覆盖的区域 int i,j,k,maxnum=0,a[n];

for(i=0,j=0;i<n;i++,j+=2){ scanf("%d %d",&a[j],&a[j+1]); if(a[j+1]>maxnum){ maxnum=a[j+1]; } }

int num[maxnum]; for(i=0;i<maxnum;i++){ num[i]=0; }

for(j=0;j<2*n;j+=2){ for(i=a[j];i<a[j+1];i++){ num[i]++; } }

//找出去除后覆盖范围最大的区间 int max=0,cnt=0; for(i=0;i<2*n;i+=2){ for(j=a[i];j<a[i+1];j++){ num[j]--; } for(k=0;k<maxnum;k++){ if(num[k]>0){ cnt++; } } if(cnt>max){ max=cnt;} for(j=a[i];j<a[i+1];j++){ num[j]++; } cnt=0; } //输出结果 printf("%d",max);

return 0; }

2024/10/18 01:13
加载中...