2020-01-13

a 夏天 提交于 2020-01-13 13:20:03

节目安排 最大兼容问题

#include

using namespace std;

#define MAX 100

int b[MAX], c[MAX] = { 0 };

struct ENT //节目开始、结束结构体
{
int start;
int end;
};

void arr(int i);

ENT a[MAX];
int n, first = 100000;
int e = 1000000;

int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i].start >> a[i].end;

arr(1);

return 0;
}

void arr(int i)
{
int tempt = 0;

if (i > n)
{
cout << "( ";
for (int k = 1; k <= n; k++)
cout << b[k] << " ";
cout << “)”<<endl;
return;
}

if (i == 1)//找最先开始的节目
{
for (int j = i;j <= n; j++)
if (first > a[j].start)
{
first = a[j].start;
tempt = j;
}
b[i] = tempt;
c[tempt] = 1;
arr(i + 1);
}

for (int j = 1; j <= n; j++)
{
if (c[j]==1||(a[b[i - 1]].end < a[j].start)||(i-1)==j) continue;

if (e > a[j].start)//求下一个节目
{
e = a[j].start;
tempt = j;
}
}
e = a[tempt].end;
b[i] = tempt;
c[tempt] = 1;
arr(i + 1);
}
希望对朋友有一些帮助,仅供参考,如有不完善之处,请多多指教,谢谢。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!