PAT (Advanced Level) 1026. Table Tennis (30)

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT (Advanced Level) 1026. Table Tennis (30)相关的知识,希望对你有一定的参考价值。

情况比较多的模拟题。

交了50发的样子才AC......AC之后我的天空星星都亮了。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
using namespace std;

struct X
{
    int st,ml,len;
    bool tag;
}s[20000+10];
int n,m,k;
bool flag[100+10],g[100+10];
int ans[100+10],w[100+10];
queue<int>P,V;

bool cmp(const X&a,const X&b) { return a.st<b.st; }
bool cmp2(const X&a,const X&b) { return a.ml<b.ml; }

void init()
{
    memset(ans,0,sizeof ans);
    memset(flag,0,sizeof flag);
    for(int i=1;i<=100;i++) w[i]=8*60*60;
}

void read()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int hh,mm,ss; scanf("%d:%d:%d",&hh,&mm,&ss);
        s[i].st=hh*3600+mm*60+ss;
        scanf("%d",&s[i].len); s[i].len=min(60*s[i].len,2*60*60);
        cin>>s[i].tag;
    }
    scanf("%d%d",&m,&k);
    for(int i=1;i<=k;i++) {int id; cin>>id; flag[id]=1;}
    sort(s+1,s+1+n,cmp);
    s[0].st=999999999;
}

void work()
{
    int sz=0,pre=0;

    while(1)
    {
        if(sz==n) break;
        int MIN=999999999;
        for(int i=1;i<=m;i++) MIN=min(MIN,w[i]);

        for(int i=pre+1;i<=n;i++)
        {
            if(s[i].st>MIN) break;
            if(s[i].tag==0) P.push(i); else V.push(i); pre=i;
        }

        memset(g,0,sizeof g);
        for(int i=1;i<=m;i++) if(w[i]==MIN) g[i]=1;

        if(P.empty()&&V.empty())
        {
            sz++; pre++;int Find=0;
            if(s[pre].tag==1)
            {
                for(int i=1;i<=m;i++)
                    if(flag[i]==1&&w[i]<=s[pre].st)
                    {
                        w[i]=s[pre].st+s[pre].len;
                        s[pre].ml=s[pre].st; Find=1;
                        if(s[pre].ml<21*60*60) ans[i]++;
                        break;
                    }
                if(Find==1) continue;
                for(int i=1;i<=m;i++)
                    if(flag[i]==0&&w[i]<=s[pre].st)
                    {
                        w[i]=s[pre].st+s[pre].len;
                        s[pre].ml=s[pre].st; Find=1;
                        if(s[pre].ml<21*60*60) ans[i]++;
                        break;
                    }
            }
            else
            {
                for(int i=1;i<=m;i++)
                    if(w[i]<=s[pre].st)
                    {
                        w[i]=s[pre].st+s[pre].len;
                        s[pre].ml=s[pre].st; Find=1;
                        if(s[pre].ml<21*60*60) ans[i]++;
                        break;
                    }
            }
            continue;
        }

        while(!V.empty())
        {
            int fail=1,id=V.front();
            for(int j=1;j<=m;j++)
                if(flag[j]&&g[j])
                {
                    sz++;
                    s[id].ml=w[j];
                    w[j]=s[id].ml+s[id].len;
                    fail=0; g[j]=0; V.pop();
                    if(s[id].ml<21*60*60) ans[j]++;
                    break;
                }
            if(fail==1) break;
        }

        while(1)
        {
            int tmp1=0,tmp2=0,id;
            if(P.empty()&&V.empty()) break;
            if(!P.empty()) tmp1=P.front(); if(!V.empty()) tmp2=V.front();
            if(s[tmp1].st<s[tmp2].st) id=tmp1; else id=tmp2;

            int fail=1;
            for(int j=1;j<=m;j++)
                if(g[j])
                {
                    sz++;
                    s[id].ml=w[j];
                    w[j]=s[id].ml+s[id].len;
                    fail=0; g[j]=0; if(s[id].tag==0) P.pop(); else V.pop();
                    if(s[id].ml<21*60*60) ans[j]++;
                    break;
                }
            if(fail==1) break;
        }
    }
}

void prin()
{
    sort(s+1,s+1+n,cmp2);
    for(int i=1;i<=n;i++)
    {
        if(s[i].ml>=21*60*60) continue;

        int fen=(s[i].ml-s[i].st+30)/60;

        printf("%02d:",s[i].st/3600); s[i].st=s[i].st-s[i].st/3600*3600;
        printf("%02d:",s[i].st/60); s[i].st=s[i].st-s[i].st/60*60;
        printf("%02d ",s[i].st);

        printf("%02d:",s[i].ml/3600); s[i].ml=s[i].ml-s[i].ml/3600*3600;
        printf("%02d:",s[i].ml/60); s[i].ml=s[i].ml-s[i].ml/60*60;
        printf("%02d ",s[i].ml);

        printf("%d\n",fen);
    }

    for(int i=1;i<=m;i++)
    {
        printf("%d",ans[i]);
        if(i<m) printf(" ");
        else printf("\n");
    }
}

int main()
{
    init();
    read();
    work();
    prin();
    return 0;
}

 

以上是关于PAT (Advanced Level) 1026. Table Tennis (30)的主要内容,如果未能解决你的问题,请参考以下文章

PAT (Advanced Level) 1026. Table Tennis (30)

PAT (Basic Level) Practice (中文) 1026 程序运行时间

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1079