您的位置:首页 > 产品设计 > UI/UE

uva 11995 I Can Guess the Data Structure!

2016-10-23 11:49 288 查看
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. 2 Take out an element from the bag. Given a sequence of operations with return values, you’re going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine! Input There are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤ 1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). Output For each test case, output one of the following: stack It’s definitely a stack. queue It’s definitely a queue. priority queue It’s definitely a priority queue. impossible It can’t be a stack, a queue or a priority queue. not sure It can be more than one of the three data structures mentioned above.

Sample Input

6 1 1 1 2 1 3 2 1 2 2 2 3 6 1 1 1 2 1 3 2 3 2 2 2 1 2 1 1 2 2 4 1 2 1 1 2 1 2 2 7 1 2 1 5 1 1 1 3 2 5 1 4 2 4

Sample Output

queue not sure impossible stack priority queue

————————————————————我是分割线——————————————————

纯模拟题目。

祝大家都ak愉快。

/*
Problem:
OJ:
User:    S.B.S.
Time:
Memory:
Length:
*/
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<functional>
#include<stack>
#include<bitset>
#include<vector>
#include<list>
#define F(i,j,k) for(int i=j;i<k;++i)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define maxn 10001
#define inf 0x3f3f3f3f
#define maxm 4001
#define mod 998244353
#define LOCAL
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
struct POINT
{
int id;
int data;
friend bool operator < (POINT a,POINT b){return a.data < b.data;}
}d[maxn];
inline bool pdstk()
{
stack<int> stk;
F(i,0,n){
if(d[i].id==1) stk.push(d[i].data);
if(d[i].id==2){
if(stk.empty()) return false;
if(stk.top()==d[i].data){stk.pop();continue;}
else return false;
}
}
return true;
}
inline bool pdque()
{
queue<int> q;
F(i,0,n){
if(d[i].id==1) q.push(d[i].data);
if(d[i].id==2){
if(q.empty()) return false;
if (q.front()==d[i].data){q.pop();continue;}
else return 0;
}
}
return true;
}
inline bool pdheap()
{
priority_queue<POINT> h;
F(i,0,n){
if (d[i].id==1) h.push(d[i]);
if(d[i].id==2){
if(h.empty()) return false;
if(h.top().data==d[i].data){h.pop();continue;}
else return 0;
}
}
return true;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("qu.in","r",stdin);
freopen("qu.out","w",stdout);
#endif
cin>>n;
F(i,0,n) cin>>d[i].id>>d[i].data;
if(pdstk()) cout<<"YES"<<endl;else cout<<"No"<<endl;
if(pdque()) cout<<"YES"<<endl;else cout<<"No"<<endl;
if(pdheap()) cout<<"YES"<<endl;else cout<<"No"<<endl;
return 0;
}


uva 11995
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: