张三大学学的是机械专业,毕业后他用有限的资金开了一家摩托车零件加工厂,专门为摩托车制造商制造零件。由于资金有限,他只能先购买一台加工机器。现在他却遇到了麻烦,多家摩托车制造商需要他加工一些不同零件(由于厂家和零件不同,所以给的加工费也不同),而且不同厂家对于不同零件的加工时间要求不同(有些加工时间要求甚至是冲突的,但开始和结束时间相同不算冲突)。
张三希望能把所有的零件都加工完,以得到更多的加工费,但当一些零件的加工时间要求有冲突时,在某个时间内他只能选择某种零件加工(因为他只有一台机器),为了赚得尽量多的加工费,张三不知如何进行取舍。
现在请你帮张三设计一个程序,合理选择部分(或全部)零件进行加工,使得得到最大的加工费。
【输入】
每组测试数据的第一行是一个整数n(n<=30000),表示共有n个零件须加工。
接下来的n行中,每行有3个整数,分别表示每个零件加工的时间要求。
第一个表示开始时间,第二个表示该零件加工的结束时间,第三个表示加工该零件可以得到的加工费。
注:数据中的每个数值不会超过100000.
【输出 】
对每组测试数据,输出一个整数,表示张三可以得到的最大加工费。
【输入样例 】
3
1 3 10
4 6 20
2 5 25
【输出样例 】
30
#include "MaxIncome.h"
#include <stdio.h>
#include <stdlib.h>
/*
加工过程
*/
/*
struct item
{
int start ;
int end ;
int cost ;
struct item *next;
};
typedef struct item * StructItem ;
*/
void print(StructItem arg) ;
int sum_cost(StructItem arg);
struct item* getValid(struct item * arg ) ; //得到工作链表
struct item* getValidItem(struct item * arg, int end) ;
struct item* getStartItem(struct item * arg) ;
int main()
{
struct item *first = NULL;
struct item *next = NULL , * tmp = NULL;
first = (struct item *) malloc(sizeof(struct item)) ;
first->start = 1 ;
first->end =3 ;
first->cost= 10 ;
tmp = (struct item*) malloc(sizeof(struct item)) ;
tmp->start = 4 ;
tmp->end =6 ;
tmp->cost= 20 ;
next = first ;
next->next = tmp ;
next = next->next ;
tmp = (struct item*) malloc(sizeof(struct item)) ;
tmp->start = 3 ;
tmp->end =5 ;
tmp->cost= 25 ;
tmp->next = NULL ;
next->next = tmp ;
next = next->next ;
tmp = (struct item*) malloc(sizeof(struct item)) ;
tmp->start = 7 ;
tmp->end =9 ;
tmp->cost= 25 ;
tmp->next = NULL ;
next->next = tmp ;
next = next->next ;
tmp = (struct item*) malloc(sizeof(struct item)) ;
tmp->start = 1 ;
tmp->end =2 ;
tmp->cost= 30 ;
tmp->next = NULL ;
next->next = tmp ;
print(first) ;
tmp = getValid(first) ;//getStartItem(first) ;
printf("Max cost: %d\n", sum_cost(tmp) );
print(tmp) ;
return 0 ;
}
int sum_cost(struct item * arg)
{
if (arg != NULL)
{
struct item * tmp = arg ;
int sum = 0 ;
while (tmp != NULL)
{
sum += tmp->cost ;
tmp = tmp->next ;
}
return sum ;
}
return 0 ;
}
struct item* getValid(struct item * arg )
{
struct item * first = NULL, *tmp = NULL , *next = NULL ;
first = getStartItem(arg) ;
printf("first Item --->>> : start=%d, end=%d, cost=%d .\n", first->start, first->end, first->cost) ;
tmp = arg ;
next = first ;
while (next != NULL && tmp != NULL)
{
tmp = getValidItem(tmp, next->end) ;
next->next = getStartItem(tmp) ;
next = next->next ;
}
return first ;
}
/*
*获得当前结束时间后,可以继续工作的事项链表
*/
struct item* getValidItem(struct item * arg, int end)
{
struct item *first= NULL ;//getStartItem(arg) ;
struct item * tmp = arg, *curr = NULL , *tmp1 = NULL;
while (tmp != NULL)
{
if (tmp->start > end)
{
tmp1 = (struct item * ) malloc(sizeof(struct item)) ;
tmp1->start = tmp->start ;
tmp1->end = tmp->end ;
tmp1->cost = tmp->cost ;
tmp1->next = NULL ;
if (first == NULL)
{
first = tmp1 ;
curr=first ;
}
else
{
curr->next = tmp1 ;
curr= curr->next ;
}
}
tmp = tmp->next ;
}
return first ;
}
/*
*获得开始工作的Item
*/
struct item* getStartItem(struct item * arg)
{
struct item * validItem = NULL, *tmp = NULL ;
if (arg != NULL)
{
tmp = arg;
while (tmp != NULL)
{
if ((validItem == NULL ) || (validItem->start > tmp->start) ||
((validItem->start == tmp->start) && (validItem->cost < tmp->cost) ))
{
validItem = (struct item *) malloc(sizeof(struct item)) ;
validItem->start = tmp->start;
validItem->end = tmp->end ;
validItem->cost = tmp->cost ;
validItem->next = NULL ;
}
tmp = tmp->next ;
}
}
return validItem ;
}
/*
*打印工作链表数据
*/
void print(struct item * arg)
{
printf("--------------------------------\n") ;
if (arg != NULL)
{
struct item *tmp = arg ;
while (arg != NULL)
{
printf("start=%d, end=%d, cost=%d .\n", arg->start, arg->end, arg->cost) ;
arg = arg->next ;
}
}else{
printf("argument is NULL.\n") ;
}
printf("--------------------------------\n") ;
}
--------------------------------
start=1, end=3, cost=10 .
start=4, end=6, cost=20 .
start=2, end=5, cost=25 .
start=7, end=9, cost=25 .
start=1, end=2, cost=30 .
--------------------------------
first Item --->>> : start=1, end=2, cost=30 .
Max cost: 75
--------------------------------
start=1, end=2, cost=30 .
start=4, end=6, cost=20 .
start=7, end=9, cost=25 .
--------------------------------
Press any key to continue