貪心:高階鐘點祕書-會議安排 c++
阿新 • • 發佈:2018-12-21
在最短的時間召開更多的會議。會議有開始時間和結束時間。
分析:按照結束時間sort,統計就行了,一個個放進揹包裡,統計數量。
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int M=10000000; struct Meet{ int beg; int end; int num; }meet[M]; class setMeet{ public: void init(); void solve(); private: int n,ans; }; void setMeet::init(){ int s,e; cout<<"input total number:"<<endl; cin>>n; int i; cout<<"start time and end time:"<<endl; for(i=0;i<n;i++){ cin>>s>>e; meet[i].beg=s; meet[i].end=e; meet[i].num=i; } } bool cmp(Meet x, Meet y){ if(x.end == y.end) return x.beg>y.beg; return x.end<y.end; } void setMeet::solve(){ sort(meet,meet+n,cmp); cout<<"sorted result:"<<endl; cout<<"number"<<"start time"<<"end time"<<endl; for(int i=0;i<n;i++){ cout<<" "<<meet[i].num<<"\t\t"<<meet[i].beg<<"\t"<<meet[i].end<<endl; } cout<<" procedure"<<endl; cout<<" chose "<<meet[0].num<<" meetig"<<endl; ans=1; int last=meet[0].end; for(int i=1;i<n;i++){ if(meet[i].beg>last){ ans++; last=meet[i].end; cout<<" chose "<<meet[i].num<<" meetig"<<endl; } } cout<<"max number"<<ans<<endl; } int main(){ setMeet sm; sm.init(); sm.solve(); return 0; }