背包问题《会议问题》修订
#include<iostream>
#include<algorithm>
using namespace std;
struct Meet
{
int beg;
int end;
int num;
}meet[1000];
class setMeet
{
public:
void init();
void solve();
private:
int n, ans;
};
void setMeet::init()
{
int s, e;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s >> e;
meet[i].beg = s;
meet[i].end = e;
meet[i].num = i + 1;
}
}
bool cmp(Meet a, Meet b)
{
if (a.end == b.end)
{
return a.beg > b.beg;
}
return a.end < b.end;
}
void setMeet::solve()
{
sort(meet, meet + n, cmp);
int Ibegin = 0;
for (int i = 0; i < n; i++)
{
if (Ibegin + 1 <= meet[i].beg)
{
Ibegin = meet[i].end;
cout << meet[i].num << " ";
}
}
}
int main()
{
setMeet m;
m.init();
m.solve();
return 0;
}
修改的地方在排序上面
bool cmp(Meet a, Meet b)
{
if (a.end == b.end)
{
return a.beg > b.beg;
}
return a.end < b.end;
}
这样排序及按结束时间从小到大排序。当遇到结束时间相同时,以开始时间大的排在前面!
这样有利于减小不必要的循环!