PAT 1016(一个点也没有过Σ( ° △ °|||)︴,不知道问题出在哪里)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
#define MinSize (5)
int TimeCost[24];
using namespace std;
typedef struct HeapStruct *BinaryHeap;
typedef struct AVLtree *Node;
typedef struct AVLtree *Position;
typedef struct customer *Element;
typedef struct call *ElementType;
struct customer{
	char name[20];
	double charge;
	BinaryHeap h;
};
struct call{
	int month;
	int day;
	int min;
	int hour;
	int flag;
};
struct charge{
	struct call begin,end;
	double cost;
	int totaltime; 
};
struct AVLtree{
	Element x;
	int height;
	Node left,right;
};
struct HeapStruct{
	int Capacity;
	int size;
	ElementType *Elements;
};
int cmp(struct call *c,struct call *d){
	struct call a,b;
	a = *c;
	b = *d;
	if(a.month == b.month)
		if(a.day == b.day)
			if(a.hour == b.hour)
				return a.min - b.min;
			else return a.hour - b.hour;
		else return a.day - b.day;
	else return a.month - b.month;
}
BinaryHeap Initialize( int NumElements )
{
    BinaryHeap H;
    struct call *MinData;
    if( NumElements<MinSize )
		perror( "Priority queue size is too small" );
    H = (BinaryHeap)malloc( sizeof( struct HeapStruct ) );
    if( H ==NULL )
	perror( "Out of space!!!" );
    	H->Elements = (ElementType*)malloc( ( NumElements + 1 )* sizeof( ElementType ) );
    if( H->Elements == NULL )
		perror( "Out of space!!!" );
    H->Capacity = NumElements;
     H->size = 0;
     MinData = (struct call*)malloc(sizeof(struct call));
     MinData->month = 0;
    H->Elements[ 0 ] = MinData;
    return H;
}
void Insert( ElementType X, BinaryHeap H )
{
    int i;

    if( H->size == H->Capacity)
    {
		perror( "Priority queue is full" );
		return;
    }

    for( i = ++H->size; cmp(H->Elements[ i / 2 ] , X)>0; i /= 2 )
		H->Elements[ i ] = H->Elements[ i / 2 ];
    H->Elements[ i ] = X;
}

ElementType
        DeleteMin( BinaryHeap H )
        {
            int i, Child;
            ElementType MinElement, LastElement;

      if( H->size == 0 )
            {
          perror( "Priority queue is empty" );
          return H->Elements[ 0 ];
            }
      MinElement = H->Elements[ 1 ];
      LastElement = H->Elements[ H->size-- ];

      for( i = 1; i * 2 <= H->size; i = Child )
            {
                /* Find smaller child */
          Child = i * 2;
           if( Child != H->size && cmp(H->Elements[ Child + 1 ], H->Elements[ Child ])<0 )
              Child++;
                /* Percolate one level */
          if( cmp(LastElement , H->Elements[ Child ])>0 )
              H->Elements[ i ] = H->Elements[ Child ];
                else
              break;
            }
      H->Elements[ i ] = LastElement;
      return MinElement;
 }
int max(int a,int b){
	return a>b?a:b;
}
int height(Node t){
	if(t)
		return t->height;
	else return -1;
}
Position SingleRotationWithRight(Node t){
	Node tmp;
	tmp = t->left;
	t->left = tmp->right;
	tmp->right = t;
	t->height = max(height(t->left),height(t->right)) + 1;
	tmp->height = max(height(tmp->left),height(tmp->right)) + 1;
	return tmp;
}
Position SingleRotationWithLeft(Node t){
	Node tmp;
	tmp = t->right;
	t->right = tmp->left;
	tmp->left = t;
	t->height = max(height(t->left),height(t->right)) + 1;
	tmp->height = max(height(tmp->left),height(tmp->right)) + 1;
	return tmp;
}
Position DoubleRotationWithLeft(Node t){
	t->left = SingleRotationWithLeft(t->left);
	return SingleRotationWithRight(t);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
}
Position DoubleRotationWithRight(Node t){
	t->right = SingleRotationWithRight(t->right);
	return SingleRotationWithLeft(t->right);
}
Position AVLInsert(Element x,Node t){
	if(t == NULL){
		t = (Node)malloc(sizeof(struct AVLtree));
		if(t == NULL){
			perror("Out of Space\n");
			return NULL;
		}
		t->left = t->right = NULL;
		t->x = x;
		t->height = 0;
	}
	else if(strcmp(x->name,t->x->name)<0){
		t->left = AVLInsert(x,t->left);
		if(height(t->left) - height(t->right)>1)
			if(strcmp(x->name,t->left->x->name)<0)
				t = SingleRotationWithRight(t);
			else t = DoubleRotationWithLeft(t);
	}
	else if(strcmp(x->name,t->x->name)>0){
		t->right = AVLInsert(x,t->right);
		if(height(t->right) - height(t->left)>1)
			if(strcmp(x->name,t->right->x->name)>0)
				t = SingleRotationWithLeft(t);
			else t = DoubleRotationWithRight(t);
	}
	t->height = max(height(t->left),height(t->right)) + 1;
	return t;
}
Position AVLFind(char *name,Node t){
	while(t!=NULL){
		if(strcmp(name,t->x->name) == 0)
			break;
		if(strcmp(name,t->x->name)>0)
			t = t->right;
		else t = t->left;
	}
	return t;
}
Position InsertRecord(char *name,struct call *NewRecord,Node t){
	Node tmp;
	Element NewNode;
	tmp = AVLFind(name,t);
	if(!tmp){
		NewNode = (Element)malloc(sizeof(struct customer));
		strcpy(NewNode->name,name);
		NewNode->charge = 0;
		NewNode->h = Initialize(500);
		Insert(NewRecord,NewNode->h);
		t = AVLInsert(NewNode,t);

	}
	else Insert(NewRecord,tmp->x->h);
	return t;
}
void PrintInt(int a){
	if(a<10)
		printf("0%d",a);
	else printf("%d",a);
}
void PrintTime(struct call a){
	PrintInt(a.day);
	printf(":");
	PrintInt(a.hour);
	printf(":");
	PrintInt(a.min);
}
queue<struct charge> q;
double CountCharge(struct call *c,struct call *d){
	struct call a,b;
	a = *c;
	b = *d;
	struct charge record;
	record.begin = a;
	record.end = b;
	int TmCst = (b.day - a.day)*24*60 + (b.hour - a.hour)*60 + b.min - a.min;
	record.totaltime = TmCst;
	double Cost = 0;
	if(TmCst<(60 - a.min))
	{
		Cost = TmCst*TimeCost[a.hour-1];
		TmCst = 0;
	}
	else {
		Cost = TimeCost[a.hour]*(60-a.min);
		a.hour++;
		if(a.hour == 24)
			a.hour = 0;
		TmCst -= 60 - a.min;
	}
	while(TmCst){
		if(TmCst/60){
			Cost += TimeCost[a.hour]*60;
			a.hour++;
			if(a.hour == 24)
				a.hour = 0;
			TmCst -= 60;
		}
		else {
			Cost += TimeCost[a.hour]*TmCst;
			TmCst = 0;
		}
	}
	record.cost = Cost/100;
	q.push(record);
	return Cost/100;
}
void PrintRecords(Node t){
	struct call *tmp1,*tmp2;
	int month;
	struct charge record;
	int flag;
	if(t){
		PrintRecords(t->left);
		flag = 0;
		month = t->x->h->Elements[1]->month;
		while(t->x->h->size){
			tmp1 = DeleteMin(t->x->h);
			if(!t->x->h->size)
				break;
			tmp2 = DeleteMin(t->x->h);
			if(tmp1->flag == 1&&tmp2->flag == 0){
				t->x->charge += CountCharge(tmp1,tmp2);
				flag++;
			}
			else if(tmp2->flag == 1){
				tmp1 = tmp2;
				if(!t->x->h->size)
					break;
				tmp2 = DeleteMin(t->x->h);
				t->x->charge += CountCharge(tmp1,tmp2);
				flag++;
			}
			else continue;
		}
		if(flag){
			printf("%s",t->x->name);
			if(month<10)
				printf(" 0%d\n",month);
			else printf(" %d\n",month);
			while(!q.empty()){
				record = q.front();
				q.pop();
				PrintTime(record.begin);
				printf(" ");
				PrintTime(record.end);
				printf(" %d $%.2lf\n",record.totaltime,record.cost);
			}
			printf("Total amount: $%.2lf\n",t->x->charge);
		}
		PrintRecords(t->right);
	}
}
void Dispose(Node t){
	if(t){
		Dispose(t->left);
		Dispose(t->right);
		free(t->x->h->Elements);
		free(t->x->h);
		free(t->x);
		free(t);
	}
}
int main(){
	int i;
	Node t;
	t = NULL;
	freopen("1.in","r",stdin);
	for(i = 0;i<24;i++)
		scanf("%d",&TimeCost[i]);
	int NumOfRecords;
	scanf("%d",&NumOfRecords);
	char name[20],cmd[20];
	int day,month,hour,min;
	struct call *NewRecord;
	for(i = 0;i<NumOfRecords;i++){
		scanf("%s",name);
		scanf("%d:%d:%d:%d",&month,&day,&hour,&min);
		scanf("%s",cmd);
		NewRecord = (struct call*)malloc(sizeof(struct call));
		NewRecord->month = month;
		NewRecord->day = day;
		NewRecord->hour = hour;
		NewRecord->min = min;
		if(strcmp(cmd,"on-line") == 0)
			NewRecord->flag = 1;
		else NewRecord->flag = 0;
		t = InsertRecord(name,NewRecord,t);
	}
	PrintRecords(t);
	Dispose(t);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值