zoukankan      html  css  js  c++  java
  • 九度 1463 招聘会(任务调度, 贪心算法)

    题目描述:

    又到毕业季,很多大公司来学校招聘,招聘会分散在不同时间段,小明想知道自己最多能完整的参加多少个招聘会(参加一个招聘会的时候不能中断或离开)。

    思路

    算法导论贪心算法章节原题, 编程之美上也有类似题目.

    这种招聘会还比较简单, 招聘会本身没有权值, 假如考虑权值, 题目就变成动态规划了

    先对招聘会按照截止时间排序, 然后按照截止时间选取具体哪场招聘会, 时间复杂度为 O(nlogn)

    代码

    #include <iostream>
    #include <stdio.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    class Meeting {
    public:
        int st, ed;
    
        Meeting(int _st, int _ed):st(_st), ed(_ed) {
        }
    
        Meeting() {
            Meeting(0, 0);
        }
        bool operator<(const Meeting &ths) const{
            if(this->ed != ths.ed)
                return this->ed < ths.ed;
            return this->st < ths.st;
        }
    };
    
    int main() {
        int n;
        while(scanf("%d", &n) != EOF) {
            vector<Meeting> meetings;
            for(int i = 0; i < n; i ++) {
                int st, ed;
                scanf("%d%d", &st, &ed);
                meetings.push_back(Meeting(st, ed));
            }
    
            sort(meetings.begin(), meetings.end());
    
            int res = 0;
            int lastpos = 0;
            for(int i = 0; i < meetings.size(); i ++) {
                if(meetings[i].st >= lastpos) {
                    res ++;
                    lastpos = meetings[i].ed;
                }
            }
            printf("%d
    ", res);
        }
    
        return 0;
    }
  • 相关阅读:
    Pandas 学习记录(一)
    python 列表常用操作
    pandas 基本操作
    Numpy np.array 相关常用操作学习笔记
    JS控制背景音乐 没有界面
    Linux Awk使用案例总结
    Yii2 定时任务创建(Console 任务)
    YII2项目常用技能知识总结
    /etc/fstab readyonly 解决办法
    Redis 排行榜 自己简单练习
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3594175.html
Copyright © 2011-2022 走看看