UVa 540 - Team Queue

此题坑死我了,刚开始理解错题意了,卡了两天,连WA带RE了十几遍,最坑爹的是,从网上找的所有测试数据竟然全都过了,但始终不对,最终问了一高手,给了一数据,没过,具体问了问,结果是理解错题意了,思路全部错了,根据题意从新整理思路写下来,终于AC了~~~~

大体题意就是,让你排一个长队,ENQUEUE -X 就是往队列里加一个元素,如果这个元素跟队列里的某个元素在上边给出的team里是在相同的team,则将这个元素插在这个元素的右边,否则放在总队列的最后 ~

在这里可以用一个二维队列,只要是碰到同一队的可以将其放到其相应的子队列后边。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
struct team
{
    int a[1000+2],fn;
} TT[1000+2];                  // 存给出的team;
typedef struct Ele_List
{
    int elem;
    struct Ele_List *next;
} ELE;
struct LIST_L
{
    int et;
    ELE *head,*rear;
} l_st[100000+2];          //定义一个结构体数组,每个结构体里面放一个链表,用于存储子队列
int mem_sort[1000+10],a[200000],ct,ffct;
int find(int ele,int num)
{
    for(int i=1; i<=num; i++)
        for(int j=0; j<TT[i].fn; j++)
            if(ele==TT[i].a[j])
                return i;
    return 0;
}
int De_find()
{
    if(l_st[ffct].et>0)
    {
        ELE *cur=l_st[ffct].head;
        cout<<l_st[ffct].head->elem<<endl;
        l_st[ffct].head=cur->next;
        free(cur);
        l_st[ffct].et--;
    }
    else if(l_st[ffct].et==0&&ct==0)
        return 0;
    if(l_st[ffct].et==0)
    {
        int ff=a[ffct];
        mem_sort[ff]=-1;
        ffct++;
    }
    return 0;
}
int list_creat(int cc,int ele)
{
    ELE *cur=(struct Ele_List *)malloc(sizeof(struct Ele_List));
    cur->next=NULL;
    cur->elem=ele;
    if(l_st[cc].et==0)
        l_st[cc].head=cur;
    else
        l_st[cc].rear->next=cur;
    l_st[cc].rear=cur;
    l_st[cc].et++;
    return 0;
}
void commands(int num)
{
    string b;
    int ele;
    ffct=ct=0;
    for(int i=0;i<1002;i++)
        mem_sort[i]=-1;        //初始化
    memset(a,0,sizeof(a));
    while(1)
    {
        cin>>b;
        if(b=="ENQUEUE")
        {
            cin>>ele;
            if(ct==0)
            {
                int fee=find(ele,num);//查找元素ele所在team中的位置
                a[ct]=fee;            //存储队列元素所对应的team的标号
                mem_sort[fee]=ct;     //标记该元素在team的大队列中出现过
                l_st[ct].et=0;
                list_creat(ct,ele);
                ct++;
            }
            else
            {
                int fee=find(ele,num);
                if(mem_sort[fee]>-1) //如果总队列里有相应的元素,则将新元素放到其对应的子队列中
                {
                    int cct=mem_sort[fee];
                    list_creat(cct,ele);
                }
                else if(mem_sort[fee]==-1)   //否则,新建一个子队列
                {
                    l_st[ct].et=0;
                    list_creat(ct,ele);
                    mem_sort[fee]=ct;
                    a[ct]=fee;
                    ct++;
                }
            }
        }
        else if(b=="DEQUEUE")
        {
            De_find();
        }
        else if(b=="STOP")
            break;
    }
}
int main()
{
#ifdef test
    freopen("sample.txt","r",stdin);
#endif
    int num,ct=1;
    while(cin >> num)
    {
        if(num == 0)
            break;
        for(int i=1; i<=num; i++)
        {
            cin >> TT[i].fn;
            for(int j=0; j<TT[i].fn; j++)
            {
                cin>>TT[i].a[j];
            }
        }
        cout<<"Scenario #"<<ct++<<endl;
        commands(num);
        cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值