首页 > 其他分享 >P2952 [USACO09OPEN] Cow Line S

P2952 [USACO09OPEN] Cow Line S

时间:2024-10-22 20:17:45浏览次数:3  
标签:USACO09OPEN idx Cow int cows Line 奶牛 line left

[USACO09OPEN] Cow Line S

题面翻译

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

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

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

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

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

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

【输入格式】

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

【输出格式】

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

【样例解释】

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

  • A L: 1 1 1;
  • A L: 2 , 1 2,1 2,1;
  • A R: 2 , 1 , 3 2,1,3 2,1,3;
  • A L: 4 , 2 , 1 , 3 4,2,1,3 4,2,1,3;
  • D R 2: 4 , 2 4,2 4,2;
  • A R: 4 , 2 , 5 4,2,5 4,2,5;
  • A R: 4 , 2 , 5 , 6 4,2,5,6 4,2,5,6;
  • D L 1: 2 , 5 , 6 2,5,6 2,5,6;
  • A L: 7 , 2 , 5 , 6 7,2,5,6 7,2,5,6;
  • A R(最终序列): 7 , 2 , 5 , 6 , 8 7,2,5,6,8 7,2,5,6,8。

题目描述

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.

样例 #1

样例输入 #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 <iostream>

using namespace std;

const int N = 100010;

int l[N],r[N],e[N], idx;


void init()
{
	r[0] = 1;
	l[1] = 0;
	idx = 2;
}

//在第k个数字的右边加上一个奶牛
void add(int k)
{
	e[idx] = idx - 1;
	l[idx] = k;
	r[idx] = r[k];

	l[r[k]] = idx;
	r[k] = idx;
	idx++;
}

//将第k个插入的数字删除
void left(int k)
{
	l[r[k]] = l[k];
	r[l[k]] = r[k];
}

int main()
{
	init();
	int s; cin >> s;
	while (s--)
	{
		char x,y;
		int z;
		cin >> x;
		if (x == 'A')
		{
			cin >> y;
			//一头奶牛从队列左边加入
			if (y == 'L') add(0);
			else add(l[1]);//一头奶牛从队列右边加入
		}
		else if (x == 'D')
		{
			cin >> y >> z;
			if (y == 'L')
			{
				for(  int i = 0;i < z;i++) 
				   left(r[0]);//K 头奶牛从队伍左边离开
			}
			else {
				for (int i = 0; i < z; i++)
					left(l[1]);
			}
		}
	}
	for (int i = r[0]; i != 1; i = r[i])
		cout << e[i] << "\n";
	return 0;
}

标签:USACO09OPEN,idx,Cow,int,cows,Line,奶牛,line,left
From: https://blog.csdn.net/weixin_73378557/article/details/143126314

相关文章

  • Shebang/hashbang/bang line -指定解释器
    Shebang(也称为hashbang或bangline)是一个在脚本文件的第一行用来指定解释器的特殊字符序列。它的语法如下:#!/path/to/interpreter解释#!:表示这是一个shebang行。#是注释符号,!是感叹号,组合在一起表示后面的内容是执行该脚本所需的解释器。/path/to/interpreter:这......
  • 用PyQt5中的textline实现log的实时显示
    在PyQt5中使用QLineEdit(即QTextLine的实现类之一)来实现日志的实时显示是可行的,但可能不适合大规模、多行日志的输出,因为QLineEdit仅支持单行文本。若要显示多行日志,建议使用QTextEdit,它更适合日志实时显示。但如果你确实希望使用QLineEdit来实现简单的日志输出,可以通......
  • pipeline scm方式
    pipelinescm方式通过scm获取Jenkinsfile1.代码仓库gitlab上项目根目录里包含jenkinsfile文件(可重命名)2.jenkinsjobpipeline作业里选择scm及git配置,路径,文件名称####我们在Jenkins新建一个pipelinejob,命名为My-pipeline-job01,前2步,同上一个示例一样,在M......
  • P6189 [NOI Online #1 入门组] 跑步(分拆数)
    简要题意给你一个整数\(n\),你需要求\(\sum_{i=1}^nx_i=n\)且\(x_i\lex_{i+1}\)的非负整数解数量对给定模数\(p\)取模后的结果。\(n\le10^5\)分析考虑一个显然的DP:设\(f_{i,j}\)表示考虑\(1\simi\)这些数,总和为\(j\)的方案数。转移是完全背包型转移:\(f_{i,j}......
  • 2024 ICPC Asia Taiwan Online Programming Contest题解记录
    比赛链接:https://codeforces.com/gym/105383/problemA.AnimalFarm找个最大pig,然后所有比他小的其他种类生物一直加就好了#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllmod=1e9+7;llksm(llx,lly){ llans=1; while(y) { if(y&1)......
  • abc248E K-colinear Line
    给定二维平面上的N个不同的点,坐标分别为(X[i],Y[i]),问存在多少条直线穿过至少K个点?1<=K<=N<=300;|X[i]|,|Y[i]|<=1E9分析:最多只有300个点,可以枚举所有点对构成的直线,用斜率和截距表示,为了避免精度问题,两者用分数来表示。注意,平行与x轴和y轴的直线要特判处理。#include<bits/std......
  • Regular graph and line graph (正则图和线图)(二)
    (1)循环矩阵的定义:一个矩阵称为循环矩阵,如果它的元素满足,其中下标是模约化的,并且位于集合(2),循环矩阵可由基本循环矩阵线性表出(3),循环矩阵的特征值(4)循环图的定义:图是一个循环图,它的顶点可以排序,使得邻接矩阵是一个循环矩阵(5)第一行为的邻接矩阵的循环图的特征值为:(6)可以计算3......
  • Regular graph and line graph (正则图和线图)(一)
    (1)正则图的定义:如果一个图的每个顶点的度数都是,则称这个图是正则的。(2)正则图的性质:命题1、命题2和推论1命题1:设是度正则图,则:是的特征值;如果是连通的,那么的重数为1;对于的任何特征值,我们有.命题2:矩阵属于邻接代数当且仅当是正则连通图.推论1:设是阶正则连通图,设的不同特征......
  • The 2024 ICPC Asia East Continent Online Contest (II)打题+写题笔记
    前言方队让我们来打于是来打。赛时2h过了AFGIJL,感谢qsq贡献的G。评价:A:唐,F:唐,G:没看,I:小清新构造,J:国王游戏,L:不做评价。补题补了C,EEEscape链接题意给你\(n\)个波特和一个人与一张无向联通图,波特有一个共同的活动距离\(d\)。不能在原地不动。问人在保证不遇到波特的情况下......
  • 洛谷 P2886 [USACO07NOV] Cow Relays G 做题记录
    设矩阵\(M^1=\begin{bmatrix}dis_{1,1}&\dots&dis_{1,n}\\\vdots&\ddots&\vdots\\dis_{1,n}&\cdots&dis_{n,n}\end{bmatrix}\),其中\(dis_{i,j}\)表示\(i\)是否能在\(1\)步内走到\(j\)。让我们回忆一下矩阵乘法,\(c_{i,j......