hdu1837(優先隊列)看病要排隊

來源:互聯網
上載者:User
分析:
    優先隊列水題。
    注意優先隊列要定義在大while裡面,等效於每次都
初始化為空白。

#include<stdio.h>
#include<string.h>
#include<queue>
char a[100];
using namespace std;
struct point
{
 int x,y;
 friend bool operator<(point a,point b)
 {
  if(a.y!=b.y)
   return a.y<b.y;-----優先順序高的優先。。
  return a.x>b.x;等的時間短的優先。。
 }
};
int main()
{
 int m,n,k,i,j,p;
 point cur;
 while(scanf("%d",&m)!=EOF)
 {
  priority_queue<point>q[4];----------q【4】很好。。
  j=1;
  for(i=0;i<m;i++)
  {
   scanf("%s",a);
   if(strcmp(a,"IN")==0)
   {
    scanf("%d%d",&p,&k);
    cur.x=j;
    cur.y=k;
    q[p].push(cur);、、入隊、、
    j++;
   }
   else if(strcmp(a,"OUT")==0)
   {
    
    scanf("%d",&n);
    if(q[n].empty())-----如果為空白
     printf("EMPTY\n");
    else
    { 
     cur=q[n].top();
     q[n].pop();
     printf("%d\n",cur.x);
    }
   }
  }
 }
 return 0;

普通方法: 結構體((不過數大的話會逾時))#include<stdio.h>
#include<string.h>
struct point
{
    int x,y,z;
}p[10000];
int main()
{
    int m,i,j,k,h,r,t;
    char a[100];
    while(scanf("%d",&m)!=EOF)
    {
        t=1;
        for(i=1;i<=m;i++)
        {
              scanf("%s",a);
            if(strcmp(a,"IN")==0)
            {
                scanf("%d%d",&p[t].x,&p[t].y);
                p[t].z=t;
                t++;
            }
            else
            {
                scanf("%d",&k);
                h=0,r=0;
                for(j=1;j<t;j++)
                {
                    if(p[j].x==k&&p[j].y>h)
                    {
                        h=p[j].y;
                        r=j;
                    }
                }
                p[r].y=0;
                if(h!=0)
                printf("%d\n",p[r].z);
                else
                    printf("EMPTY\n");
            }
        }
    }
    return 0;
}

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.