貪心演算法-作用選取範圍

來源:互聯網
上載者:User

問題描述
問題來自演算法導論16.1。

幾個相互競爭的活動進行調度,他們要求以獨佔的方式使用某一公用資源。調度的目標是找出一個最大的相互相容活動集合。

思想:總是選擇剩餘活動中具有最早結束時間的活動。

/*****************************************************************************Copyright: 2012, USTCFile name: main.cppDescription:Arrange activities by greedy method.Author:Silang QuanVersion: 1.0Date: 2012.12.3*****************************************************************************/#include<iostream>#include<vector>using namespace std;//Iteration Method assume a[1] is the first activity meet the need.int GreedySelect(int *s,int *f,int length,int *a){int i,j=2;a[1]=1;i=1;for(int m=2;m<=length;m++){if(s[m]>=f[i]){a[j++]=m;i=m;}}return j;}//Recursion methodvoid GreedySelect2(int *s,int *f,int i,int j,vector<int>& result)// const int started[], const int finished[],vector<int>& result, int i,int j){    if( i >= j ) return;    int l;    for( l = i+1; l < j; ++l )    {        if( s[l] >= f[i] )        {            result.push_back(l);            break;        }    }    GreedySelect2(s,f,l,j,result);}int main(){int s[12]={0,1,3,0,5,3,5,6,8,8,2,12};int f[12]={0,4,5,6,7,8,9,10,11,12,13,14};//test1/*int a[13];int i=GreedySelect(s,f,11,a);for(int j=1;j<i;j++){cout<<a[j]<<" ";}cout<<endl;*///test2    vector<int> result;    GreedySelect2(s,f,0,12,result);    for(vector<int>::iterator iter=result.begin();iter!=result.end();++iter)    {        cout<<*iter<<" ";    }    cout<<endl;}

參考:

演算法導論,第二版,機械工業出版社

演算法導論-貪心演算法-http://blog.csdn.net/liuzhanchen1987/article/details/7854826

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.