CodeForces-982B Bus of Characters 队列+栈+思维

记录下来是因为...我好菜啊55555

看了题解发现好巧妙啊...

同样先贴大佬博客Orz:https://blog.csdn.net/Zero_979/article/details/80385545

就是先把座位从小到大排列一下,遇到0压入队列并压入栈,遇到1就读取栈的top并压入队列

附上AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int MAX=200005;
int n;
struct seat
{
    int w;
    int id;
}w[MAX];
char p[2*MAX];
bool cmp(struct seat a,struct seat b)
{
    return a.w<b.w;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&w[i].w);
            w[i].id=i+1;
        }
        scanf("%s",p);
        sort(w,w+n,cmp);
        int len=strlen(p);
        queue<int>q;
        stack<struct seat>s1;
        int t=0;
        for(int i=0;i<len;i++)
        {
            if(p[i]=='0')
            {
                q.push(w[t].id);
                s1.push(w[t]);
                t++;
            }
            else
            {
                struct seat k=s1.top();
                s1.pop();
                q.push(k.id);
            }
        }
        while(!q.empty())
        {
            int k=q.front();
            q.pop();
            printf("%d ",k);
        }
        printf("\n");
    }
    return 0;
}

接下来附上我一开始写的超时的代码QAQ

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAX=200005;
int n;
struct seat0
{
    int w;
    int id;
}s0[MAX];
struct seat1
{
    int w;
    int id;
}s1[MAX];
int vis[MAX];
char peo[2*MAX];

bool cmp0(struct seat0 a,struct seat0 b)
{
    return a.w<b.w;
}
bool cmp1(struct seat1 a,struct seat1 b)
{
    return a.w>b.w;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&s0[i].w);
            s0[i].id=i;
            s1[i].w=s0[i].w;
            s1[i].id=i;
        }
        scanf("%s",peo+1);
        int len=strlen(peo+1);
        sort(s0+1,s0+1+n,cmp0);
        sort(s1+1,s1+1+n,cmp1);
        int ans[2*MAX];
        memset(vis,0,sizeof(vis));
        if(peo[1]=='0')
            ans[1]=s0[1].id;
        else
            ans[1]=s1[1].id;
        vis[ans[1]]=1;
        for(int i=2;i<=len;i++)
        {
            //cout<<"i="<<i<<" peo="<<peo[i]<<endl;
            if(peo[i]=='0')
            {
                for(int j=1;j<=n;j++)
                {
                    int k=s0[j].id;
                    //cout<<"j="<<j<<" id="<<k<<" vis="<<vis[k]<<endl;
                    if(!vis[k])
                    {
                        vis[k]++;
                        ans[i]=k;
                        break;
                    }
                }
            }
            else
            {
                for(int j=1;j<=n;j++)
                {
                    int k=s1[j].id;
                    //cout<<"j="<<j<<" id="<<k<<" vis="<<vis[k]<<endl;
                    if(vis[k]==1)
                    {
                        vis[k]++;
                        ans[i]=k;
                        break;
                    }
                }
            }
        }
        for(int i=1;i<len;i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[len]);
    }
    return 0;
}

我好菜啊qaq还要多刷题呐555

猜你喜欢

转载自blog.csdn.net/Cc_Sonia/article/details/81315547