站外题求助
  • 板块学术版
  • 楼主xiaoshumiao
  • 当前回复8
  • 已保存回复8
  • 发布时间2024/3/11 11:21
  • 上次更新2024/3/11 16:00:49
查看原帖
站外题求助
1008513
xiaoshumiao楼主2024/3/11 11:21

有一条长度为 LL 的马路需要打扫。

有 NN 个打扫工人,第 ii 个工人打扫 [Si,Ti][S_i,T_i] 范围内的马路。(马路从 11 到 LL)

最少用多少个工人能把马路打扫干净,无解输出 −1-1。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=25010;
struct Sweeper { int s,t; }a[N];
bool cmp(Sweeper a,Sweeper b) { return (a.s==b.s?a.t>b.t:a.s<b.s); }
int main() {
  int n,l; scanf("%d %d",&n,&l);
  for(int i=1;i<=n;i++) scanf("%d %d",&a[i].s,&a[i].t);
  sort(a+1,a+n+1,cmp);
  if(a[1].s>1) return printf("-1"),0;
  int cur=1,cnt=1;
  while(true) {
    int tmp=cur;
    while(a[cur].t<=a[tmp].t&&cur<=n) cur++;
    if(cur>n) return printf("-1"),0;
    cnt++;
    if(a[cur].t==l) return printf("%d",cnt),0;
  }
  printf("-1");
  return 0;
}

样例:

1 7
3 6
6 10
------
2
2024/3/11 11:21
加载中...