P2952 [USACO09OPEN]牛线Cow Line

题目描述

Farmer John's N cows (conveniently numbered 1..N) are forming a line. The line begins with no cows and then, as time progresses, one by one, the cows join the line on the left or right side. Every once in a while, some number of cows on the left or right side of the line all leave the line to go graze in their favorite pasture.

FJ has trouble keeping track of all the cows in the line. Please help him.

The cows enter the line in numerical order 1..N, and once a cow leaves the line she never re-enters it. Your program will be given S (1 <= S <= 100,000) input specifications; each appears on a single line and is one of two types:

  • A cow enters the line (a parameter indicates whether on the left or right).

  • K cows leave the line from the left or right side (supplied parameters define both the number of cows and which side).

Input lines never request an operation that can not be performed.

After all the input lines have been processed, your program should print the cows in the line in order from left to right. The final line is guaranteed to be non-empty at the end of the input

specifications.

约翰的N只奶牛(编为1到N号)正在直线上排队.直线上开始的时候一只牛也没有.接下来发生了S(1≤S≤100000)次事件,一次事件可能是以下四种情况之一:

.一只奶牛加入队伍的左边(输入“AL”).

.一只奶牛加入队伍的右边(输入“AR”).

·K只队伍左边奶牛离开(输入“DLK”).

·K只队伍右边奶牛离开(输入“DRK”).

请求出最后的队伍是什么样.

数据保证离开的奶牛不会超过队伍里的奶牛数,最后的队伍不空

输入输出格式

输入格式:

  • Line 1: A single integer: S

  • Lines 2..S+1: Line i+1 contains specification i in one of four formats:

  • A L -- a cow arrives on the Left of the line

  • A R -- a cow arrives on the Right of the line

  • D L K -- K cows depart the Left side of the line

  • D R K -- K cows depart the Right side of the line

第1行输入S,之后S行每行描述一次事件,格式如题目描述所示

输出格式:

  • Lines 1..??: Print the numbers of the cows in the line in order from left to right, one number per line.

由左到右输出队伍最后的情况.

输入输出样例

输入样例#1: 复制

10
A L
A L
A R
A L
D R 2
A R
A R
D L 1
A L
A R
输出样例#1: 复制

7
2
5
6
8

说明

Input Resulting Cow Line

A L 1

A L 2 1

A R 2 1 3

A L 4 2 1 3

D R 2 4 2

A R 4 2 5

A R 4 2 5 6

D L 1 2 5 6

A L 7 2 5 6

A R 7 2 5 6 8

感谢@ ws_fuweidong 提供翻译。

双端对列

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
char ch;
deque<int>q;
int n,k,s,ans;
int read()
{
    ,f=; char ch=getchar();
    ;ch=getchar();}
    +ch-',ch=getchar();
    return x*f;
}
int main()
{
    n=read();
    ;i<=n;i++)
    {
        cin>>ch;
        if(ch=='A')
        {
            s++;
            cin>>ch;
            if(ch=='L')
             q.push_front(s);
            else
             q.push_back(s);
        }
        else
        {
            cin>>ch;k=read();
            if(ch=='L')
              ;i<=k;i++)
                q.pop_front();
            else
              ;i<=k;i++)
                q.pop_back();
        }
    }
    while(!q.empty())
    {
        ans=q.front();
        q.pop_front();
        printf("%d\n",ans);
    }
    ;
}

洛谷——P2952 [USACO09OPEN]牛线Cow Line的更多相关文章

  1. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

  2. [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)

    如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...

  3. 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解

    题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...

  4. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  5. P3014 [USACO11FEB]牛线Cow Line && 康托展开

    康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托 ...

  6. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  7. 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery

    P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...

  8. 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

    P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...

  9. 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging

    题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...

随机推荐

  1. Maven:Non-resolvable parent POM: Failure to find错误

    使用Maven编译项目时遇到如下错误: [ERROR] The project dfjr.generic:dfjr-generic:1.0-SNAPSHOT (F:\workspace\DFJR-PE ...

  2. PHP扩展--Suhosin保护PHP应用系统

    什么是Suhosin? Suhosin是一个PHP程序的保护系统.它的设计初衷是为了保护服务器和用户抵御PHP程序和PHP核心中,已知或者未知的缺陷. Suhosin有两个独立的部分,使用时可以分开使 ...

  3. java collection(一)

    1.Collection层次结构: 2.集合Conllection的基本概念: (1)集合的基本认识:如StringBuffer&StringBuilder是集合(存储的对象类型是String ...

  4. 【vijos】P1448 校门外的树

    [题意]两种操作,[L,R]种新的树(不覆盖原来的),或查询[L,R]树的种类数.n<=50000. [算法]树状数组||线段树 [题解]这题可以用主席树实现……不过因为不覆盖原来的,所以有更简 ...

  5. 崩坏3mmd中的渲染技术研究

    http://youxiputao.com/articles/11839 主要是参考该篇文章做一个微小的复盘. 漫反射与高光 文章中的漫反射与高光并不是类似于普通的 resultCol = Diffu ...

  6. 【洛谷 P4568】 [JLOI2011]飞行路线 (分层最短路)

    题目链接 分层图最短路. 把每个点拆成\(k+1\)个点,表示总共有\(k+1\)层. 然后每层正常连边, 若\((u,v)\)有边,则把每一层的\(u\)和下一层的\(v\).每一层的\(v\)和下 ...

  7. Mimikatz.ps1本地执行

    PS C:\Users\hacker> Get-ExecutionPolicy Restricted PS C:\Users\hacker> Set-ExecutionPolicy Unr ...

  8. ERROR: do not initialise statics to false

    Question about git commit rule I git commit a patch, The patch has a "static int xxxxxxxxxxxxxx ...

  9. [How to] 动态布局可变高度的cell的应用

    1.简介 代码:https://github.com/xufeng79x/DynamicChangeableCell 微博界面,微信和QQ聊天界面,这些界面的布局大都不确定,且每一条消息的高度也不一样 ...

  10. svn服务器配置以及自动同步到web服务器

    感觉再不用svn就真的老了. 安装 yum install subversion 新建repo mkdir -p /opt/svn/myrepo svnadmin create /opt/svn/my ...