题解 | Cow Line-牛客假日团队赛9D题

D-Cow Line_牛客假日团队赛9

https://ac.nowcoder.com/acm/contest/1071/D

题目描述

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  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.

示例1

输入
10 
A L 
A L 
A R 
A L 
D R 2 
A R 
A R 
D L 1 
A L 
A R 
输出
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

解答

这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque,下面来简单讲解一下deque的一些操作。
:清空队列
:从尾部插入一个元素。
:从头部插入一个元素。
deque双端队列的先进就在这里,它可以两端都支持同样的操作。
:返回队列元素个数
:返回队列首部元素。
:返回尾部元素。
:弹出队尾元素。
:弹出队首元素。
:检查队列是否为空。
... ......
... ......
... ......
然后输出的方法多种多样,我选择使用迭代器,具体详见代码。
代码:
#include<cstdio>
#include<deque>
using namespace std;
deque<int>q;
int main()
{
	int s;
	scanf("%d\n",&s);
	int cnt=0;
	for(int j=1;j<=s;j++)
	{
		char c1,c2;
		int c3;
		scanf("%c %c",&c1,&c2);
		if(c1=='A')
		{
			if(c2=='L')
			{
				q.push_front(++cnt);
			}
			else
			{
				q.push_back(++cnt);
			}
			if(j!=s)
			scanf("\n");
		}
		else
		{
			scanf("%d",&c3);
			if(j!=s)
			scanf("\n");
			if(c2=='L')
			{
				for(int i=1;i<=c3;i++)
				{
					q.pop_front();
				} 
			}
			else
			{
				for(int i=1;i<=c3;i++)
				q.pop_back();
			}
		}
	}
	deque<int>::iterator it=q.begin();
	for(it=q.begin();it!=q.end();it++)
	{
		printf("%d\n",*it);
	}
	return 0;
}


来源:ShineEternal
全部评论

相关推荐

02-12 20:22
重庆大学 Java
字节暑期刚入职四天,因为是年前,所以很多正职都放假走了,也就没有给我分配mt,然后有一个老哥在我来的时候给我发了一个landing手册,然后还有关于部门业务的白皮书,还有一些业务代码。然后本人是java面的,进来第一次接触go语言&nbsp;前面几天熟悉了一下go的语法和go的框架,可以读但是还不太会写,然后业务白皮书也看的很头疼,包括landing手册里要了解的很多东西说实话我看文档真的快看死了,一个嵌套一个,问题是我还完全不知道咋用这个我了解的东西,还有就是那个项目代码,那个老哥喊我去写写单测,熟悉一下go的语法,但也进行的很困难(这是我第一段实习,之前都是springboot那一套,真不太熟悉这个)想问问大家的建议,就是我从现在开始到在开年回来之前应该做些什么,我目前就一个想法&nbsp;就是复现一个landing手册上的go框架小项目&nbsp;就是相当于帮自己锻炼锻炼怎么写go&nbsp;或者各位大佬有没有更好的锻炼go语法的建议还有就是大家都在说vibe&nbsp;coding,那我应该怎么锻炼自己使用ai的能力,感觉我除了给一些需求然后它给我生成代码,好像就没别的用法了,那些什么工作流、拆解、skill啥的都不知道从哪一个地方开始,包括我现在正在实习,不知道精力该怎么分配,去网上想找找关于agent开发的一些学习流程,说实话,众说纷纭,有的是从python开始打基础然后系统学那些rag&nbsp;prompt&nbsp;langchain&nbsp;mcp等等,有的是说直接找一个github上的ai项目然后反复问ai,我确实有点迷茫,恳求各位大佬能留下你们宝贵的建议,我一定认真反复深刻学习有一说一&nbsp;我觉得字节饭挺好吃的!
双非后端失败第N人:1. go语言我建议你让ai带着你先把基本语法速通了,然后再去用go重新刷你以前刷过的leetcode,这样熟悉起来很快 2. 直接看你们组go项目,里面用***比较复杂,然后把每一个语法现象都喂给ai,一点点看
字节跳动公司福利 1371人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务