040_区间调度(贪心)

来源:互联网 发布:vb.net int 编辑:程序博客网 时间:2024/05/25 23:59

经典的贪心算法的例子。算法导论里面有讲。

同样用c++的pair类型与sort实现效率较高。

题源来自《挑战程序竞赛》第二版40页。

////  040_interval.cpp//  changlle////  Created by user on 12/26/15.//  Copyright (c) 2015 user. All rights reserved.//#include <iostream>#include <algorithm>using namespace std;int n=5;int s[5]={1,2,4,6,8};int t[5]={3,5,7,9,10};int main() {        pair<int,int> list[5];        for (int i=0;i<5;i++) {        list[i].first=t[i];        list[i].second=s[i];       }        sort(list,list+n);        int tem_time=0;    int count=0;        for (int i=0;i<n;i++)    {        if (list[i].second>=tem_time) {           tem_time=list[i].first;                   count++;       }    }        cout<<count<<endl;                        return 0;}

0 0