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<iostream>
#include<cstdio>
using namespace std;
int main(){
    int s,k,w=1;
    char z;
    scanf("%d\n",&s);
    int left=s,right=s+1;
    int a[2*s+3];
    for(int i=s+1;i<=2*s;i++){
        scanf("%c ",&z);
        if(z==‘A‘){
            scanf("%c\n",&z);
            if(z==‘L‘){a[left]=w++;left--;}
            if(z==‘R‘){a[right]=w++;right++;}
            continue;
        }
        if(z==‘D‘){
            scanf("%c ",&z);
            scanf("%d\n",&k);
            if(z==‘L‘){
                left+=k;
            }
            if(z==‘R‘){
                right-=k;
            }
        }
    }
    left++;
    for(;left<right;left++){
        printf("%d\n",a[left]);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/xiongchongwen/p/11192362.html

时间: 2024-08-08 18:40:44

P2952 [USACO09OPEN]牛线Cow Line的相关文章

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

牛线Cow Line

题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line numb

洛谷P3014 [USACO11FEB]牛线Cow Line

---恢复内容开始--- 关于康托展开 与 康托逆展开 1 #include <bits/stdc++.h> 2 #define For(i, j, k) for(int i=j; i<=k; i++) 3 #define Dow(i, j, k) for(int i=j; i>=k; i--) 4 #define LL long long 5 using namespace std; 6 inline LL read() { 7 LL x = 0, f = 1; 8 char c

贪心 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

[USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题目描述 FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges. The contest organ

编程算法 - 最好牛线(Best Cow Line) 代码(C)

最好牛线(Best Cow Line) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 给定长度为N的字符串S, 要构造一个长度为N的字符串T. 反复进行如下任意操作. 从S的头部删除一个字符, 放入T的尾部; 从S的尾部删除一个字符, 放入T的尾部; 目标是要构造字典序尽可能小的字符串T. 使用贪心算法, 不断选取S首尾最小的字符, 放入T, 如果相等, 则再次向内查找, 找到内部最小的. 代码: /* * main.cpp * * Cr

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以SA 首先贪心选字典序小的 然后遇到相等的了比Rank数组,把原串倍长一下就可以比了. Code: #include <cstdio> #include <algorithm> const int N=6e4+10; char s[N],ans[N]; int sa[N],Rank[N]

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组)

[Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组) 题面 FJ打算带他的\(N(1 \leq N \leq 30,000)\)头奶牛去参加一年一度的"全美农场主大奖赛".在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. 今年,竞赛委员会在接受队伍报名时,采用了一种新的登记规则:他们把所有队伍中奶牛名字的首字母取出,按它们对应奶牛在队伍中的次序排成一列(比如说,如果FJ带去的奶牛依次为Bessie.Sylv

3403: [Usaco2009 Open]Cow Line 直线上的牛

3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 62[Submit][Status] Description 题目描述 约翰的N只奶牛(编为1到N号)正在直线上排队.直线上开始的时候一只牛也没有.接下来发生了S(1≤S≤100000)次事件,一次事件可能是以下四种情况之一: .一只奶牛加入队伍的左边(输入“AL”). .一只奶牛加入队伍的右边(输入“AR

BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )

直接用STL的的deque就好了... ---------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<deque> #define rep( i , n ) for( int i = 0 ; i <