4
0
0

依旧是贪心

2026-02-07
2026-08-13
文章摘要
|

P1803 凌乱的yyy / 线段覆盖

时间限制: 3.00s    内存限制: 512.00MB

题目背景

Python 用户可以尝试使用 pypy3 提交试题。

快 noip 了,yyy 很紧张!

题目描述

现在各大 oj 上有 n 个比赛,每个比赛的开始、结束的时间点是知道的。

yyy 认为,参加越多的比赛,noip 就能考的越好(假的)。

所以,他想知道他最多能参加几个比赛。

由于 yyy 是蒟蒻,如果要参加一个比赛必须善始善终,而且不能同时参加 2 个及以上的比赛。

输入格式

第一行是一个整数 n,接下来 n 行每行是 2 个整数 ai​,bi​ (ai​<bi​),表示比赛开始、结束的时间。

输出格式

一个整数最多参加的比赛数目。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct competition{
        int startTime;
        int endTime;
    };
bool compare(competition& a,competition& b){
        return a.endTime<b.endTime;
    }
int main(){
    int n;
    cin>>n;
    
    vector<competition> com(n);
    for(int i=0;i<n;i++){
        cin>>com[i].startTime>>com[i].endTime;
    }
    
    sort(com.begin(),com.end(),compare);
    int competitionCount = 0;
    int currentStartTime = 0;
    for(int i = 0;i<n;i++){
        if(com[i].startTime>=currentStartTime){
            currentStartTime=com[i].endTime;
            competitionCount++;
        }
    }
    cout<<competitionCount;
    return 0;
    
    
}

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或者给予支持!

评论