1. 程式人生 > >Pop Sequence(用stack就是無腦模擬

Pop Sequence(用stack就是無腦模擬

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

輸入描述:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

輸出描述:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.

輸入例子:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

輸出例子:
YES
NO
NO
YES
NO

#include<bits/stdc++.h>
#include <iostream> 
#include <map>
#include <string>
#include<vector>
#include<stack> 
using namespace std;	


int main()
{
int m,n,k;
cin>>m>>n>>k;
for(int i=0;i<k;i++)
{
	int a[n];
	for(int j=0;j<n;j++)
	cin>>a[j];
	
	stack<int> sta;
	int cnt=0,flag=1;
	
	for(int j=1;j<=n;j++)
	{
		sta.push(j);
		if(sta.size()>m)
		{
			flag=0;
			break;
		 } 
		 
		 while(!sta.empty()&&sta.top()==a[cnt])
		 {
		 	cnt++;
		 	sta.pop();
		 }
	}
	if(flag&&sta.empty())cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
}

		return 0;
 } 

相關推薦

Pop Sequencestack就是模擬

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed t

02-線性結構4 Pop Sequence 25 分)

check ace sample seq ati pre contain read push 02-線性結構4 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N num

PAT 1051 Pop Sequence 25 分)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are

1051 Pop Sequence 25 分)

1051 Pop Sequence(25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are sup

A - OO’s Sequence所有區間內其他因子的數的和)

原題: https://cn.vjudge.net/problem/HDU-5288 題意: 一個區間的貢獻為:此區間F數的個數 F數滿足:區間內其他位置沒有出現過其因子 解析: 預處理所有數的因子,vector x[i]記錄值為i的數出現在原陣列的下標 對於每個原陣

1051 Pop Sequence 25 分)模擬

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are su

【笨方法學PAT】1051 Pop Sequence 25 分)

一、題目 Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly

中國大學MOOC-陳越、何欽銘-資料結構-2018秋 02-線性結構4 Pop Sequence 25 分) 棧

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell

PTA 02-線性結構4 Pop Sequence 25 分)

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a giv

不能繼承於QObject的類一定不能使用信號槽?一個代理類進行發射行了)

-c 問題 ges object 編譯過程 報錯 第三方庫 nal 解決 首先不能繼承QObject的情況在開發中遇到得並不多,筆者在一年多的Qt項目開發中只遇到兩三次。而且都是因為引進了第三方庫導致編譯過程中報錯。 要想解決這個問題其實不難,因為筆者遇到的問題都是想定義

繞過010Editor網絡驗證python做一個仿真http server真容易,幾行代碼)

headers redirect 如果 table 本地 align cnn 破解版 resp 010Editor是一款非常強大的十六進制編輯器,尤其是它的模板功能在分析文件格式時相當好用!網上現在也有不少010Editor的破解版,如果沒錢或者舍不得花錢買授權的話,去官方

LeetCode 225 Implement Stack using Queues隊列來實現棧)*)

using man mean leetcode == peek uil erl end 翻譯 用隊列來實現棧的例如以下操作。 push(x) —— 將元素x加入進棧 pop() —— 從棧頂移除元素 top() —— 返回棧頂元素 empty() —

字符驅動之按鍵一:輪詢法)

des gpo first gist 結構體 字符 定義 char v_op 一、添加頭文件 二、構造fileoperation結構體 static struct fileoperations second_drv_fops = {   .o

在叢集環境中安裝R步驟清晰內容詳實,堪稱教程!)

在叢集環境下如何解決需要使用R語言的需求? 關鍵詞: PBS作業排程系統; HPC; R; 本文最初思路構思於2018/05,成文於2018/10/17 本文背景是博主的最新一篇論文(上位基因檢測機器學習演算法創新)臨近實驗尾聲,已經完成在模擬資料中的檢測,現在

【LeetCode題解】225_佇列實現棧Implement-Stack-using-Queues)

目錄 描述 解法一:雙佇列,入快出慢 思路 入棧(push) 出棧(pop) 檢視棧頂元素(peek) 是否為空(empty) Java 實現 Python 實現 解法二:雙佇列,入慢出

idea打war包最方便、快捷、高效、

... ... ① 專案(project)清理 ② 專案(project)編譯 ③ 模組(moudule)打包   問題補充: maven打包時報“.....找不到符號”錯誤:      

LeetCode-225-Implement Stack using Queues-M佇列實現堆疊)

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop

C#LeetCode刷題之#225-佇列實現棧Implement Stack using Queues)

問題 使用佇列實現棧的下列操作: push(x) -- 元素 x 入棧 pop() -- 移除棧頂元素 top() -- 獲取棧頂元素 empty() -- 返回棧是否為空 注意: 你只能使用佇列的基本操作-- 也就是 push to back, peek/pop f

24點遊戲演算法 遞迴暴力破解)

題目描述 問題描述:給出4個1-10的數字,通過加減乘除,得到數字為24就算勝利 輸入: 4個1-10的數字。[數字允許重複,但每個數字僅允許使用一次,測試用例保證無異常數字] 輸出: true or false 輸入描述: 輸入4個int整數 輸出描述: 返

資訊學奧林匹克競賽-複賽操作攻略檔案讀寫)

功能:該程式可以在pas檔案隔壁自動新建一個test.txt檔案,並且在這個記事本檔案裡自動寫入"helloworld"字樣。 begin assign(output,'test.txt'