2018年 “百度之星”程序设计大赛试题分析 - 初赛(A)-1002 度度熊学队列

度度熊学队列

Problem Url

Problem Description

这里写图片描述

Input

这里写图片描述

##Output
对于每组数据的每一个操作②,输出一行表示答案。
注意,如果操作②的队列是空的,就输出-1−1且不执行删除操作。

##Sample Input

2 10
1 1 1 23
1 1 0 233
2 1 1 
1 2 1 2333
1 2 1 23333
3 1 2 1
2 2 0
2 1 1
2 1 0
2 1 1

Sample Output

23
-1
2333
233
23333

提示

由于读入过大,C/C++ 选手建议使用读入优化。

一个简单的例子:

void read(int &x){
	char ch = getchar();x = 0;
	for (; ch < '0' || ch > '9'; ch = getchar());
	for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}

code

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

const unsigned maxn = 150000 + 5;
const unsigned maxn2 = 500000 + 5;
int L[maxn][maxn2];
int Len[maxn];

void read(int &x){
    char ch = getchar();x = 0;
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
}

void write(int x){
    if(x==0){putchar(48);return;}
    int len=0,dg[20];
    while(x>0){dg[++len]=x%10;x/=10;}
    for(int i=len;i>=1;i--)putchar(dg[i]+48);
}

int main()
{
    int N,Q,act,u,v,w,val;
    // scanf( "%d%d", &N ,&Q );
    read(N);
    read(Q);
    memset( Len, 0, N);
    while( Q-- ){
        read( act );
        read( u );
        switch( act ){
            case 1:
                //增加
                read( w );
                read( val );
                if( w == 0 ){
                    for (int i = Len[u] -1 ; i >=0 ; i--)
                    {
                        L[u][i+1] = L[u][i];
                    }
                    L[u][0] = val;
                } else {
                    L[u][Len[u]] = val;
                }
                Len[u] ++;
                break;
            case 2:
                read( w );
                if( !Len[u] ){
                    printf("%d\n", -1);
                } else {
                    if( w ){
                        printf("%d\n", L[u][ (Len[u]-1) ] );
                    } else {
                        printf("%d\n", L[u][0] );
                        for (int i = 0 ; i <Len[u] ; i++)
                        {
                            L[u][i] = L[u][i+1];
                        }
                    }
                    Len[u] --;
                }
                break;
            case 3:
                read( v );
                read( w );
                for(int j=0;j<Len[v];j++){
                    if( w ){
                        L[u][ (Len[u]+j) ] = L[v][ Len[v] - j - 1 ];
                    } else {
                        L[u][ (Len[u]+j) ] = L[v][j];
                    }
                }
                    Len[u] += Len[v];
                    Len[v] = 0;
                // for(int j=0;j<Len[u];j++){
                //     printf("%d ", L[u][j]);
                // }
                // printf("\n");
                break;
        }
    }
    putchar('\n');
}

猜你喜欢

转载自blog.csdn.net/kajweb/article/details/81588941