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 ...
随机推荐
- python小练习,打出1-100之间的所有偶数,设计一个函数,在桌面上创建10个文件,并以数字命名,复利计算函数
练习一:打出1-100之间的所有偶数 def even_print(): for i in range(1,101): if i % 2 == 0: print (i) even_print() #列 ...
- Python开发【第十篇】:CSS --无内容点击-不进去(一)
Python开发[第十篇]:CSS --无内容点击-不进去(一)
- Activity 【生命周期】
不同情况下的回调 我们打开应用时先后调用了onCreate()->onStart()->onResume 当我们按BACK键时,我们这个应用程序将结束,这时候我们将先后调用onPause( ...
- C#中的IO流操作(FileStream)
StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作 不用担心编码问题 using (Stream s = new FileStream(@&quo ...
- ADLINK 8158控制程序-连续运动(VB.NET)
运动平台:日脉的二维运动平台(一个旋转平台和一个滑动平台) 开发环境:VS2010 + .NET Framework + VB.NET 使用文件:pci_8158.vb motion_8158_2D. ...
- ie6-7 overflow:hidden失效问题的解决方法
即使父元素设置了overflow:hidden.解决这个bug很简单,在父元素中使用position:relative; zoom: 1;触发haslayout 即可解决该BUG.
- 设置cookie倒计时让让表单自动提交
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%&g ...
- Linux 抓取网站命令
wget -m -e robots=off -U "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/200 ...
- Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...
- Linux下面/usr/local和opt目录
1./opt This directory is reserved for all the software and add-on packages that are not part of the ...