1. 程式人生 > >洛谷 P2952 [USACO09OPEN] 牛線 Cow Line 雙向隊列deque

洛谷 P2952 [USACO09OPEN] 牛線 Cow Line 雙向隊列deque

push_back The gre operation 我們 ret whether quest indicate

題目描述

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

這個題目還是比較簡單的,我用的方法是建立一個數組和兩個指針模擬每次的操作,這裏介紹一個更簡單的做法

雙向隊列deque

這個東西就是兩邊都可以插入元素的隊列,入隊操作分別是push_front 和 push_back,出隊操作是 pop_front 和 pop_back

對於入隊的每頭牛,我們可以用一個計數器cnt來記錄,push_front(++cnt)即可

完整代碼

#include<bits/stdc++.h>
using namespace std;
deque<int>q;
int n,cnt,c;
char a,b;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
       {
        cin>>a>>b;    
        if(a==A)
        {
        if(b==L)    
        q.push_front(++cnt);    
        else
        q.push_back(++cnt);    
            }    
        else
        {
        cin>>c;
        if(b==L)    
        for(int i=1;i<=c;i++)    
           q.pop_front();
        else
        for(int i=1;i<=c;i++)    
           q.pop_back();    
            }    
            }
    while(!q.empty())cout<<q.front()<<endl,q.pop_front();
    return 0;
}

參考大佬@Sooke 的題解

洛谷 P2952 [USACO09OPEN] 牛線 Cow Line 雙向隊列deque