UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】
先看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用法】的更多相关文章
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- 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 ...
- UVA11995 I Can Guess the Data Structure!
思路 简单题,用栈,队列,优先队列直接模拟即可 代码 #include <cstdio> #include <algorithm> #include <cstring&g ...
- Css中路径data:image/png;base64的用法详解
今天查看一些网站的css中发现了 background-image:url(data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAAB ...
- Css中路径data:image/png;base64的用法详解 (转载)
大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: background-image:url(data:image/png;base64, iVBORw0KGg ...
- mysql中使用load data infile导入数据的用法
有时需要将大量数据批量写入数据库,直接使用程序语言和Sql写入往往很耗时间,其中有一种方案就是使用mysql load data infile导入文件的形式导入数据,这样可大大缩短数据导入时间. LO ...
- $.each(data, function (index, value) { })的用法;json和list<>的互相转换
在json中常常碰到这样的代码: jquery $.each(data, function (index, value) { }) 遍历处理data,可以是数组.DOM.json等,取决于直接给 ...
- 百度之星IP聚合(水题map&字符处理)
虽然题目停水的,但是好像字符处理运用的还比较合适 Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊 ...
- 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 ...
随机推荐
- oracle 添加自增索引
1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...
- INSTALL_PARSE_FAILED_MANIFEST_MALFORMED 错误
在eclipse编译好文件之后,往AVD中安装apk,报错如下:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED一般来说只需要检查AndroidManifest.xml中 ...
- jQuery的live()方法对hover事件的处理示例
hover([over,]out) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法 当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数. 当鼠标移出这个元素时,会触发指定的第二个 ...
- JS 改变input 输入框样式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- centos5.2 x86 安装 oracle 11g2r 日志
一.安装centos 二.安装ora所需的库 三.修改centos内核 四.建用户组和目录结构等 五.安装ora11g2r 六.安装sqlplus的翻页程序和help补丁 七.自启动脚本 八.常用命令 ...
- XML样本(格式没区别,但是一个有结果 一个没结果)
xml样本01<orderlist> <order> <orderid>1</orderid> <ord ...
- tableView创建方法调用的研究
当两个section的cell数量都为5的时候,方法的调用顺序: -[ViewController numberOfSectionsInTableView:] -[ViewController tab ...
- 三、C# 运算符和控制流
通常运算符划分为3大类: 一元运算符.二元运算会.三元运算符,它们对应的操作数分别是1个.2个.3 个. 结合性和优先级顺序. 二元运算会是从左向右结合的,相反赋值运算符是从右向左结合的. cha ...
- 【USACO 3.1.1】最短网络
[描述] 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的场.当然,他需要你的帮助. 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共 ...
- Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...