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;
}
时间: 2024-08-06 03:39:26

PAT (Advanced Level) 1026. Table Tennis (30)的相关文章

浙大 PAT Advanced level 1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the

Pat(Advanced Level)Practice--1026(Table Tennis)

Pat1026代码 题目描写叙述: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest n

1026 Table Tennis (30 分)

1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the sma

1026. Table Tennis (30)

A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the

PAT (Advanced Level) 1080. Graduate Admission (30)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

PAT (Advanced Level) 1004. Counting Leaves (30)

简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<vector> using namespace std; const int maxn=100+10; vector<int>g[maxn]; int n,m; int ans[maxn]; int root; i

PAT (Advanced Level) 1022. Digital Library (30)

简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<vector> using namespace std; struct X { string id; c

PAT (Advanced Level) 1091. Acute Stroke (30)

BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; int M,N,L,T; int A[70][1300][133]; bo

PAT (Advanced Level) 1111. Online Map (30)

预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,m; int st,en