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. Jenkins有用的插件

    1. Multijob plugin: 多个任务同时运行 2. ssh slave plugin: 用于安装slave? Allows to launch over ssh, using a java ...

  2. Android蓝牙通信总结

    这篇文章要达到的目标: 1.介绍在Android系统上实现蓝牙通信的过程中涉及到的概念. 2.在android系统上实现蓝牙通信的步骤. 3.在代码实现上的考虑. 4.例子代码实现(手持设备和蓝牙串口 ...

  3. UOJ#204 【APIO2016】Boat

    Time Limit: 70 Sec  Memory Limit: 256 MBSubmit: 559  Solved: 248 Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向 ...

  4. CodeForces527D. Fuzzy Search

    time limit per test:3 seconds memory limit per test:256 megabytes input:standard input output:standa ...

  5. Html5学习2(Html表格、Html列表、Html5新元素、Canvas (坐标、路径、画圆、文本、渐变、图像))

    Html表格 1.表格中的表头:<th></th>.其中表头部分字体加粗,颜色深绿色 <h4>水平标题:</h4> <table border=& ...

  6. FileReader 与canvas结合使用显示图片

    话不多少,直接上代码 function fileChange() { var file = this.files[0]; var imageType = /^image\//; //是否是图片 if ...

  7. Javascript prototype 及 继承机制的设计思想

    我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类"的概念,也没有"类"(class)和"实例" ...

  8. bzoj 1261 区间DP

    首先我们知道ans=Σ(h[i]*f[i])=Σ(h[i]*d[i])/s=Σ(k(r[i]+1)+c)*d[i]/s=Σ(k*r[i]+(k+c))*d[i]/s 我们可以发现,除了k*r[i]之外 ...

  9. ie8下trim失效

    1.ie8下使用trim失效 trim可以除去字符串两侧的空白字符,但ie8并不支持 2.解决方案 String.prototype.trim = function () { return this ...

  10. ubuntu tomcat的安装与配置

    一.下载jdk 大概是tomat大部分是由java写的, 所以一开始安装tomcat必须得配置好jdk http://www.oracle.com/technetwork/java/javase/do ...