Activity selection Problem (DP solution 2)


#include <stdio.h>

#define MAX_ACTIVITY_NUM	100

struct activity
{
	int start_time;
	int end_time;
};

static struct activity g_activities[MAX_ACTIVITY_NUM];
static int g_solution[MAX_ACTIVITY_NUM][MAX_ACTIVITY_NUM];
static int g_count;

static int g_temp[MAX_ACTIVITY_NUM][MAX_ACTIVITY_NUM];

struct time
{
	int left, right;
};

static struct time g_auxTime[MAX_ACTIVITY_NUM][MAX_ACTIVITY_NUM];
int find_partition(struct activity *pActivities, int p, int q)
{
	int data = pActivities[q].end_time;
	int i = p-1, j;
	
	for(j = p; j < q; j++)
	{
		if(pActivities[j].end_time < data)
		{
			i++;
			struct activity tmp = pActivities[j];
			pActivities[j] = pActivities[i];
			pActivities[i] = tmp;
		}
	}

	i++;
	struct activity tmp = pActivities[q];
	pActivities[q] = pActivities[i];
	pActivities[i] = tmp;

	return i;
}

void quick_sort(struct activity *pActivities, int i, int j)
{
	if(i < j)
	{
		int p = find_partition(pActivities, i, j);
		quick_sort(pActivities, i, p -1);
		quick_sort(pActivities, p+1, j);
	}
}


void find_optimal_activities(struct activity *pActivities, int start, int end)
{
	int len, i, j, k;
	for(len = 0; len <= end - start; len++)
	{
		for(i = start; i <= end - len; i++)
		{
			j = i + len;
			int max = 0;
			int pos = i;
			struct time t;
			for( k = i; k <= j; k++)
			{
				int temp_i = k;
				int temp_j = k;
				while(temp_i >= i && pActivities[temp_i].end_time > pActivities[k].start_time)
				{
					temp_i--;
				}
				
				while(temp_j <= j && pActivities[temp_j].start_time < pActivities[k].end_time)
				{
					temp_j++;
				}

				int temp = g_temp[i][temp_i] + g_temp[temp_j][j] + 1;

				if(max < temp)
				{
					max = temp;
					pos = k;
					t.left = temp_i;
					t.right = temp_j;
				}
			}

			g_temp[i][j] = max;
			g_solution[i][j] = pos;
			g_auxTime[i][j] = t;
		}
	}
}

void print_solution(int start, int end)
{
	if( start > end )
	{
		return;
	}

	if( start == end )
	{
		printf("[%d, %d]: %d\n", start, end, g_solution[start][end]);
		return;
	}
	printf("[%d, %d]: %d\n", start, end, g_solution[start][end]);
	print_solution(start, g_auxTime[start][end].left);
	print_solution(g_auxTime[start][end].right, end);
}


int main(void)
{
	freopen("data.txt", "r", stdin);

	int count;
	scanf("%d", &count);
	int p;	
	for(p = 0; p < count; p++)
	{
		scanf("%d", &g_count);
		int i;
		for(i = 0; i < g_count; i++)
		{
			scanf("%d %d", &g_activities[i].start_time, &g_activities[i].end_time);
		}

		quick_sort(g_activities, 0, g_count - 1 );

		int j;
		for( i = 0; i < MAX_ACTIVITY_NUM; i++)
		{
			for(j = 0; j < MAX_ACTIVITY_NUM; j++)
			{
				g_temp[i][j] = 0;
				g_solution[i][j] = 0;
			}
		}

		find_optimal_activities(g_activities, 0,  g_count-1);
		printf("max count of compatible activities: %d\n", g_temp[0][g_count-1]);
		print_solution(0, g_count-1 );
	}
	return 0;

}

http://blog.csdn.net/ii1245712564/article/details/45420061

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值