先看UVA11995

两份代码一份直接用C写的,一份用STL写的

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
//#include <priority_queue>
using namespace std;
int a[1005];
int b[1005];
int c[1005];
struct ope
{
int x;
int y;
}op[1005];
int v[4];//1队列 2栈 3优先队列
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(v,0,sizeof(v));
int cnt1=0,cnt2=0,cnt3=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&op[i].x,&op[i].y);
}
for(int i=0;i<n;i++)
{
if(op[i].x==1)
{
a[cnt1++]=op[i].y;
}
if(op[i].x==2)
{
if(cnt1-1<0) { v[1]=1;}
else
{
int temp=0;
for(int jj=0;jj<cnt1;jj++)
{
if(a[jj]==0){temp++; continue ;}
else
{
if(a[jj]==op[i].y)
{
a[jj]=0;
break;
}
else {v[1]=1;break;}
}
}
if(temp==cnt1) v[1]=1;
}
}
if(op[i].x==1)
{
b[cnt2++]=op[i].y;
}
if(op[i].x==2)
{
int temp=0;
if(cnt2-1<0) {v[2]=1;}
else
{
for(int jj=cnt2-1;jj>=0;jj--)
{
if(b[jj]==0) {temp++;continue;}
else
{
if(b[jj]==op[i].y)
{b[jj]=0;break;}
else v[2]=1;break; }
}
if(temp==cnt2) {v[2]=1;} }
}
if(op[i].x==1)
{
c[cnt3++]=op[i].y;
}
if(op[i].x==2)
{
if(cnt3-1<0) v[3]=1;
else
{
int temp3,max=-1;
for(int jj=0;jj<cnt3;jj++)
if(c[jj]>max){temp3=jj; max=c[jj];}
if(max==op[i].y) c[temp3]=0;
else v[3]=1;
}
} }
if(!v[1]&&v[2]==1&&v[3]==1)
printf("queue\n");
else if(v[1]==1&&!v[2]&&v[3]==1)
printf("stack\n");
else if(v[1]==1&&v[2]==1&&!v[3])
printf("priority queue\n");
else if(v[1]==1&&v[2]==1&&v[3]==1)
printf("impossible\n");
else printf("not sure\n");
}
return 0;
} STL版~ #include<cstdio>
#include<stack>
#include<queue>
using namespace std;
const int maxn = 1000+100;
int id[maxn],x[maxn],n;
bool isStack(){
stack<int> s;
for(int i=0;i<n;i++){
if(id[i]==1) s.push(x[i]);
else{
if(s.empty()) return false;
int val=s.top(); s.pop();
if(x[i]!=val) return false;
}
}
return true;
}
bool isQueue(){
queue<int >q;
for(int i=0;i<n;i++){
if(id[i]==1) q.push(x[i]);
else{
if(q.empty()) return false;
int val=q.front(); q.pop();
if(x[i]!=val) return false;
}
}
return true; }
bool isPriority(){
priority_queue<int > q;
for(int i=0;i<n;i++){
if(id[i]==1) q.push(x[i]);
else{
if(q.empty()) return false;
int val=q.top(); q.pop();
if(x[i]!=val) return false;
}
}
return true;
}
int main(){
while(scanf("%d",&n)!=EOF){
bool st=false,qu=false,pr=false;
for(int i=0;i<n;i++){
scanf("%d %d",&id[i],&x[i]);
}
st=isStack(); qu=isQueue(); pr=isPriority();
if(!st&&!qu&&!pr) puts("impossible");
else if((!st&&qu&&pr)||(!qu&&st&&pr)||(!pr&&qu&&st)||pr&&qu&&st){
puts("not sure");
}
else if(st) puts("stack");
else if(qu) puts("queue");
else if(pr) puts("priority queue"); }
return 0;
}

queue,stack,priority_queue取顶部or底部元素,front,top,back,push,pop.......

接下来是UVA11991

附代码

#include<iostream>
#include<vector>
#include<map>
#include<stdio.h>
using namespace std;
map<int,vector<int> > a;
int main()
{
int n,m,x,y;
while(scanf("%d%d",&n,&m)==2)
{
a.clear();
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(!a.count(x))
a[x]=vector<int>();
a[x].push_back(i+1);
}
while(m--)
{
scanf("%d%d",&x,&y);
if(!a.count(y)||a[y].size()<x)
printf("0\n");
else printf("%d\n",a[y][x-1]);
}
}
return 0;
}

map,vector用法。。

map里面的count用法。。

UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】的更多相关文章

  1. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  2. uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)

    11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...

  3. UVA11995 I Can Guess the Data Structure!

    思路 简单题,用栈,队列,优先队列直接模拟即可 代码 #include <cstdio> #include <algorithm> #include <cstring&g ...

  4. Css中路径data:image/png;base64的用法详解

    今天查看一些网站的css中发现了 background-image:url(data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAAB ...

  5. Css中路径data:image/png;base64的用法详解 (转载)

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: background-image:url(data:image/png;base64, iVBORw0KGg ...

  6. mysql中使用load data infile导入数据的用法

    有时需要将大量数据批量写入数据库,直接使用程序语言和Sql写入往往很耗时间,其中有一种方案就是使用mysql load data infile导入文件的形式导入数据,这样可大大缩短数据导入时间. LO ...

  7. $.each(data, function (index, value) { })的用法;json和list<>的互相转换

    在json中常常碰到这样的代码: jquery $.each(data, function (index, value) {    }) 遍历处理data,可以是数组.DOM.json等,取决于直接给 ...

  8. 百度之星IP聚合(水题map&字符处理)

    虽然题目停水的,但是好像字符处理运用的还比较合适 Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊 ...

  9. codeforces 659C C. Tanya and Toys(水题+map)

    题目链接: C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. SPOJ 3048 - DNA Sequences LCS

    给出两个字符串(不长于1000),求最长公共子序列,要求:从每个串中取必须取连续k (1<=k<=100)个数 [LCS]一开始自己想用DP加一维[len]用来表示当前已经取了连续len个 ...

  2. 新版本ButterKnife的配置

    新版本的ButterKnife的添加方式发生了变化,然后摸索着记录了一下. 按照ButterKnife的官网描述,使用ButterKnife需要在Gradle中添加如下依赖: compile 'com ...

  3. ADO.NET基础

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  4. access的时间相关的查询

    string sql = "select * from CONCURRENCY WHERE CONCURRENCY.DATE_FLAG BETWEEN  cdate('2013-11-1', ...

  5. Ajax请求传递参数遇到的问题

    想写个同类型的,代码未测. 什么是WebAPI?我的理解是WebAPI+JQuery(前端)基本上能完成Web MVC的功能,即:这么理解吧,WebAPI相当于Web MVC的后台部分. 接下来直接上 ...

  6. Android onConfigurationChanged(Configuration cfg) 无法触发问题

     1.android:configChanges="orientation|keyboardHidden"的使用  当在activity加上android:configChange ...

  7. UITouch触摸事件

    UITouch触摸事件 主要为三个方法 1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{2.3. UITouch * ...

  8. Asp.net IsPostBack

    Page.IsPostBack是一个标志:当前请求是否第一次打开.调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者this.Page.IsPos ...

  9. 动态加载下拉框列表并添加onclick事件

    1.  js动态加载元素并设置属性 摘自(http://www.liangshunet.com/ca/201408/336848696.htm) <div id="parent&quo ...

  10. mybatis 学习笔记(4) —— 批量新增数据

    1.业务是从前台传入List<T> ,在controller层接受参数,并进行批量新增操作. 2.需要处理的细节 a) mybatis可以支持批量新增,注意数据表需要将主键设置成自增列. ...