洛谷 P2952 [USACO09OPEN] Cow Line S

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.

输入格式

* 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

输出格式

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

题意翻译

Farmer John(以下简称 FJ)的 𝑁N 头奶牛(用 1…𝑁1…N 编号)在直线上排队。一开始,这条线上没有任何奶牛,随着时间的推移,奶牛们会一个接一个地站到队伍的左边或右边。又过了一会儿,某些奶牛会从队伍里离开,去吃自己最喜欢的草料。

FJ 无法跟踪每一头奶牛,于是,他想让你来帮助他。

奶牛以 1…𝑁1…N 的顺序排队,并且离开的奶牛不会再次回来。数据将会给出 𝑆S(1≤𝑆≤1000001≤S≤100000) 条指令,各占一行,分两种:

  • 𝐴A 头奶牛加入了队列(还有一个参数,表示从左加入还是从右加入);
  • 𝐾K 头奶牛从左边或者右边离开了队列(还有两个参数,分别表示从左离开还是从右离开和离开多少头奶牛)。

输入的命令一定是可以执行的。

所有的操作结束后,你的程序应该以从左到右的顺序输出这个奶牛队列。数据保证最后的队列不空。

【输入格式】

  • 第 11 行:单独一个整数 𝑆S。
  • 第 2…𝑆+12…S+1 行:第 𝑖+1i+1 行会有一条命令,有以下几种:
    • A L:一头奶牛从队列左边加入;
    • A R:一头奶牛从队列右边加入;
    • D L K:𝐾K 头奶牛从队伍左边离开;
    • D R K:𝐾K 头奶牛从队伍右边离开。

【输出格式】

  • 第 1…??1…?? 行:从左到右输出最后的奶牛队列,一个奶牛编号占一行。

【样例解释】

以下为输入的命令及对应的队列:

  • A L:11;
  • A L:2,12,1;
  • A R:2,1,32,1,3;
  • A L:4,2,1,34,2,1,3;
  • D R 2:4,24,2;
  • A R:4,2,54,2,5;
  • A R:4,2,5,64,2,5,6;
  • D L 1:2,5,62,5,6;
  • A L:7,2,5,67,2,5,6;
  • A R(最终序列):7,2,5,6,87,2,5,6,8。

输入输出样例

输入 #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

话不多说开始答题:(源码在最后)

首先,题目没有提示数据范,开个longlong保险点:

#include <bits/stdc++.h>
#define int long long

创建几个变量。

int n;//输入的指令条数
int cnt = 1;//奶牛编号
deque<int> q;//存奶牛的双向队列

输入。

cin >> n;
    for (int i = 1; i <= n; i++) {
        string z1, z2;
        int z3;
        cin >> z1;

输入指令(由于输入内容不定,所以用分支语句更方便)。

if (z1 == "A") {
            cin >> z2;
            if (z2 == "L") {//如果指令是A L
                q.push_front(cnt);//从队列左边加入奶牛的序号
                cnt++;
            } else if (z2 == "R") {//如果指令是A R
                q.push_back(cnt);//从队列右边加入奶牛的序号
                cnt++;
            }
        } else if (z1 == "D") {
            cin >> z2 >> z3;
            if (z2 == "L") {//如果指令是D L 数字
                for (int j = 1; j <= z3; j++) {//从队列左边进行z3次出队
                    q.pop_front();
                }
            } else if (z2 == "R") {//如果指令是D R 数字
                for (int j = 1; j <= z3; j++) {//从队列右边进行z3次出队
                    q.pop_back();
                }
            }
        }

输出。

for (auto it = q.begin(); it != q.end(); ++it) {
        cout << *it << endl;
    }

最后的总代码>w<

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int cnt = 1;
deque<int> q;

signed main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        string z1, z2;
        int z3;
        cin >> z1;
        if (z1 == "A") {
            cin >> z2;
            if (z2 == "L") {
                q.push_front(cnt);
                cnt++;
            } else if (z2 == "R") {
                q.push_back(cnt);
                cnt++;
            }
        } else if (z1 == "D") {
            cin >> z2 >> z3;
            if (z2 == "L") {
                for (int j = 1; j <= z3; j++) {
                    q.pop_front();
                }
            } else if (z2 == "R") {
                for (int j = 1; j <= z3; j++) {
                    q.pop_back();
                }
            }
        }
    }
    // 输出结果
    for (auto it = q.begin(); it != q.end(); ++it) {
        cout << *it << endl;
    }
    return 0;
}

以上代码亲测AC,希望大家给个支持>~<。

如果有不懂的地方欢迎私信我看到后会解答!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值