HDOJ 1873 看病要排队 (结构体快排)

题目链接:( ¯ □ ¯ )


思路:用结构体排序, 按优先权从小到大, 如果优先权一样按ID号从大到小排


code:

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
	int level, num;
}node;
node  quene[4][4004];
int count1 = 0, count2 = 0, count3 = 0;
int cmp(void const *a, void const *b)//结构体快排
{
	if((*(node *)a).level != (*(node *)b).level)
		return (*(node *)a).level - (*(node *)b).level;
	else
		return (-((*(node *)a).num - (*(node *)b).num));
}
int main()
{
	int n = 0, a = 0, b = 0, sum = 0;
	char str[5];
	while(scanf("%d",&n) != EOF)
	{
		count1 = count2 = count3 = sum = 0;
		while(n--)
		{
			scanf("%s",str);
			if(str[0] == 'I')
			{
				scanf("%d %d",&a, &b);
				sum++;
				if(a == 1)
				{
					quene[a][count1].level = b;
					quene[a][count1++].num = sum;
				}
				else if(a == 2)
				{
					quene[a][count2].level = b;
					quene[a][count2++].num = sum;
				}
				else
				{
					quene[a][count3].level = b;
					quene[a][count3++].num = sum;
				}
			}
			else
			{
				scanf("%d",&a);
				if(a == 1)
				{
					if(count1)
					{
						qsort(quene[a], count1, sizeof(quene[a][1]), cmp);
						printf("%d\n",quene[a][count1-1].num);
						count1--;
					}
					else
					{
						printf("EMPTY\n");
					}
				}
				else if(a == 2)
				{
					if(count2)
					{
						qsort(quene[a], count2, sizeof(quene[a][1]), cmp);
						printf("%d\n",quene[a][count2-1].num);
						count2--;
					}
					else
					{
						printf("EMPTY\n");
					}
				}
				else if(a == 3)
				{
					if(count3)
					{
						qsort(quene[a], count3, sizeof(quene[a][1]), cmp);
						printf("%d\n",quene[a][count3-1].num);
						count3--;
					}
					else
					{
						printf("EMPTY\n");
					}
				}
			}
		}
	}
	return 0;
}


你可能感兴趣的:(struct)