洛谷——P2952 [USACO09OPEN]牛线Cow Line
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.
由左到右输出队伍最后的情况.
输入输出样例
说明
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的更多相关文章
- 洛谷 P3014 [USACO11FEB]牛线Cow Line
P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...
- [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)
如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里: 简陋的博客 百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...
- 洛谷:P2952 [USACO09OPEN]牛线Cow Line:题解
题目链接:https://www.luogu.org/problemnew/show/P2952 分析: 这道题非常适合练习deque双端队列,~~既然是是练习的板子题了,建议大家还是练练deque, ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
- P3014 [USACO11FEB]牛线Cow Line && 康托展开
康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托 ...
- 洛谷P3045 [USACO12FEB]牛券Cow Coupons
P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...
- 洛谷P2950 [USACO09OPEN]牛绣Bovine Embroidery
P2950 [USACO09OPEN]牛绣Bovine Embroidery 题目描述 Bessie has taken up the detailed art of bovine embroider ...
- 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver
P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...
- 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging
题目描述 Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn ...
随机推荐
- hdu4085 Peach Blossom Spring
Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...
- LightOJ 1129 - Consistency Checker Trie树模板
题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...
- 2015/8/18 Python基本使用(2)
关于判断和循环语句 Python的判断和循环语句非常直观,读起来很接近自然语言. 判断语句if标准的if语句是如下结构: if expression: if_suite 如果expression的表达 ...
- [洛谷P1404] 平均数
洛谷题目链接:平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来 ...
- 持续集成之配置环境创建JOB
1.安装mvn.gitlab插件 2.配置参数构建
- 【BZOJ3998】弦论 [SAM]
弦论 Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 对于一个给定长度为N的字符串,求它的第 ...
- 【51NOD-0】1011 最大公约数GCD
[算法]欧几里德算法 #include<cstdio> int gcd(int a,int b) {?a:gcd(b,a%b);} int main() { int a,b; scanf( ...
- PHP is_null,empty以及isset,unset的区别
1.empty 判断一个变量是否为“空”.null.false.00.0.’0′.』.为以上值的变量在检测時都将返回true. 2.isset 判断一个变量是否已经设置.0.00.’0′.』.’ ‘. ...
- 深入理解Spring MVC(山东数漫江湖)
初始工程 使用Spring Boot和web,thymeleaf的starter来设置初始工程.xml配置如下: <parent> <groupId>org.springf ...
- 访问dubbo没有权限,通过ip进行跳转服务器,并通过有权限服务器代理访问
#启动ip跳转 echo 1 > /proc/sys/net/ipv4/ip_forward vi /etc/sysctl.conf net.ipv4.ip_forward =1 sysctl ...