zoukankan      html  css  js  c++  java
  • HDU 5071:Chat(2014 Asia AnShan Regional Contest)

    Chat

    题目链接:

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5071

    题意:

    给出8种操作,输出对应的结果

    题解:

    挺麻烦的一个模拟题,仔细看操作就能A,数据不大暴力就行了,注意结束的时候输出Bye要先输出always on top的情况,不存在某人关闭了聊天框又打开的情况。

    代码

    #include<stdio.h>
    #include<queue>
    #include<math.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    const int N=5001;
    char s[10];
    struct node
    {
        int p;
        long long time;
    }q[N];
    int len,top,w;
    void Untop()
    {
        printf("Operation #%d: ",w);
        if(top==-1)printf("no such person.
    ");
        else
        {
            printf("success.
    ");
            top=-1;
        }
    }
    void Chat()
    {
        long long x;
        scanf("%lld",&x);
        printf("Operation #%d: ",w);
        if(!len)printf("empty.
    ");
        else
        {
            bool u=false;
            if(top!=-1)
            {
                for(int i=1;i<=len;++i)
                if(q[i].p==top)
                {
                    q[i].time+=x;
                    u=true;
                    break;
                }
            }
            if(!u)q[1].time+=x;
            printf("success.
    ");
        }
    }
    void Prior()
    {
        printf("Operation #%d: ",w);
        if(!len)printf("empty.
    ");
        else
        {
            printf("success.
    ");
            node ss=q[1];
            int vv=1;
            for(int i=2;i<=len;++i)
            if(q[i].p>ss.p)ss=q[i],vv=i;
            for(int i=vv;i>=2;--i)
            q[i]=q[i-1];
            q[1]=ss;
        }
    }
    void Add()
    {
        int x;
        scanf("%d",&x);
        printf("Operation #%d: ",w);
        bool u=false;
        for(int i=1;i<=len;++i)
        if(q[i].p==x)
        {
            u=true;
            break;
        }
        if(u)printf("same priority.
    ");
        else
        {
            printf("success.
    ");
            q[++len].p=x;
            q[len].time=0;
        }
    }
    void Close()
    {
        long long u=-1;
        int vv,x;
        scanf("%d",&x);
        printf("Operation #%d: ",w);
        for(int i=1;i<=len;++i)
        if(q[i].p==x)
        {
            u=q[i].time;
            vv=i;
            break;
        }
        if(u==-1)printf("invalid priority.
    ");
        else
        {
            printf("close %d with %lld.
    ",x,u);
            for(int i=vv;i<len;++i)
            q[i]=q[i+1];
            len--;
        }
    }
    void Rotate()
    {
        int x;
        scanf("%d",&x);
        printf("Operation #%d: ",w);
        if(x<1||x>len)printf("out of range.
    ");
        else
        {
            printf("success.
    ");
            q[0]=q[x];
            for(int i=x;i>1;--i)
            q[i]=q[i-1];
            q[1]=q[0];
        }
    }
    void Choose()
    {
        int u=-1,vv,x;
        scanf("%d",&x);
        printf("Operation #%d: ",w);
        for(int i=1;i<=len;++i)
        if(q[i].p==x)
        {
            u=q[i].time;
            vv=i;
            break;
        }
        if(u==-1)printf("invalid priority.
    ");
        else
        {
            printf("success.
    ");
            q[0]=q[vv];
            for(int i=vv;i>1;--i)
            q[i]=q[i-1];
            q[1]=q[0]; 
        }
    }
    void Top()
    {
        int x;
        bool u=true;
        scanf("%d",&x);
        printf("Operation #%d: ",w);
        for(int i=1;i<=len;++i)
        if(q[i].p==x)
        {
            u=false;
            break;
        }
        if(u)printf("invalid priority.
    ");
        else
        {
            printf("success.
    ");
            top=x;
        }
    }
    void solve()
    {
        int T,n;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            len=0;
            top=-1;
            int x;
            for(w=1;w<=n;++w)
            {
                scanf("%s",s);
                if(s[0]=='U')Untop();
                if(!strcmp(s,"Chat"))Chat();
                if(s[0]=='P')Prior();
                if(s[0]=='A')Add();
                if(!strcmp(s,"Close"))Close();
                if(s[0]=='R')Rotate();
                if(!strcmp(s,"Choose"))Choose();
                if(s[0]=='T')Top();
            }
            if(top!=-1)
            {
                for(int i=1;i<=len;++i)
                if(q[i].p==top&&q[i].time>0)
                {
                    printf("Bye %d: %lld
    ",top,q[i].time);
                    break;
                }
            }
            for(int i=1;i<=len;++i)
            if(q[i].time>0&&q[i].p!=top)printf("Bye %d: %lld
    ",q[i].p,q[i].time);
        }
    }
    int main()
    {
        solve();
        return 0;
    }
  • 相关阅读:
    利用FT232实现USB转串口
    用例图
    Flash与EEPROM
    spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现
    得到中文首字母
    删除目录
    数字格式化工具
    Md5加密
    Double类型的数值 在写入excel时 如何去掉 科学计算法的 后面数值+ E的 情况
    数值的比较
  • 原文地址:https://www.cnblogs.com/kiuhghcsc/p/5826470.html
Copyright © 2011-2022 走看看