STL初步

提交ACM会TLE /仅以学习STL与迭代器使用

C. Cards Sorting

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.

Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.

You are to determine the total number of times Vasily takes the top card from the deck.

Input

<font size=3 face"微软雅黑">The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.

The second line contains a sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), where ai is the number written on the i-th from top card in the deck.

Output

Print the total number of times Vasily takes the top card from the deck.

Examples

input

4

6 3 1 2

output

7

input

1

1000

output

1

input*

7

3 3 3 3 3 3 3

output

7

暴力代码

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
queue<int>q;
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
q.push(a[i]);
}
sort(a,a+n);
int j=0;
for(int i=1;;i++)
{
if(q.front()==a[j])
{
q.pop();
j++;
if(q.empty())
{printf("%d\n",i);
return 0;
}
}
else
{
q.push(q.front());
q.pop();
}
} }

STL是指C++标准模板库。它很好用,也很复杂。还原题目的意图显而易见,运用库函数,依靠队列进行模拟摸牌过程。

queue库函数

STL队列定义在头文件中,可以用queues 的方式声明一个队列。用fush()和pop()进行入队与出队,front()取队首元素但不删除。

队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。

还原题意既是取对头一张牌符合条件就pop出队头第一张,不符合就将第一张pop出后插入至队尾。

接着只需要判断front()与当前最小值是否相等即可,最小值可以用中sort排序或者或者的迭代器进行判断得出。

set库函数

集合:set

集合(set)与映射(map)是两个经常用的容器。set就是数学上的集合,每个元素至多出现一次,和sort一样,set支持内部排序。set内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree),RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。

这里有两点需要注意1、set中的元素都是排好序的 。2、set集合中没有重复的元素。即使你输入了两个相同的数,它也仅仅是将原来的变量覆盖掉。

begin()        ,返回set容器第一个元素的迭代器

end()      ,返回一个指向当前set末尾元素的下一位置的迭代器.

clear()          ,删除set容器中的所有的元素

empty()    ,判断set容器是否为空

max_size()   ,返回set容器可能包含的元素最大个数

size()      ,返回当前set容器中的元素个数

rbegin()     ,返回的值和end()相同

rend()     ,返回的值和begin()相同

map 库函数

映射(map)就是从键(key)到值(value)的映射。因为重载了[]运算符,map更像是数组的“高级版”。

map的简单声明:map<int,string>a 例如可以用一个map<string,int>month_name来表示“月份名字到月份编号”的映射,用month_name["july"]=7这样来赋值,当然赋值的方法有很多种,如

a.insert(pair<int, string>(000, "july"));

a.insert(map<int, string>::value_type(001, "month"));

a[1] = "july";

定义一个map的迭代器时需要声明map<int,int>std::iterator a1;

在map迭代器中根据输入的格式不同,输入相同的“key”会导致map迭代器本身处理相同变量的方法不同。

使用q1.insert(pair<int,int>(a[i],i));方式进行输入,key值相同则进行不操作(忽略)进行下一步,而使用a[1] = "july";这种格式键入则会对上一个重复的key值进行覆盖,也就是将新的value覆盖到旧的value上。

在map迭代器中内部会对key进行排序,下面是使用map判断最小值进行的代码。

使用map的暴力代码

#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
queue<int>q;
map<int,int>q1;
map <int, int>::iterator q1_Iter;
int main(){ int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
q.push(a[i]);
q1.insert(pair<int,int>(a[i],i));
}
int j=0;
q1_Iter = q1.begin( ); for(int i=1;;i++)
{ if(q.front()==q1_Iter->first)
{
q1_Iter++;
int h=q1_Iter->first;
if(h)--q1_Iter;
q.pop();
j++;
if(q.empty())
{
printf("%d\n",i);
return 0;
}
}
else
{
q.push(q.front());
q.pop();
}
} }

#C++初学记录(STL容器以及迭代器)的更多相关文章

  1. STL(标准模板库)理论基础,容器,迭代器,算法

    基本概念 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间.   ...

  2. STL容器迭代器失效分析

    连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效, ...

  3. stl中的容器、迭代器和算法----vector中的find实现

    来源 http://blog.csdn.net/huangyimin/article/details/6133650 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的 ...

  4. stl之容器、迭代器、算法几者之间的关系

    转自:https://blog.csdn.net/bobodem/article/details/49386131 stl包括容器.迭代器和算法: 容器 用于管理一些相关的数据类型.每种容器都有它的优 ...

  5. STL容器是否是线程安全的

    转载http://blog.csdn.net/zdl1016/article/details/5941330 STL的线程安全. 说一些关于stl容器的线程安全相关的话题. 一般说来,stl对于多线程 ...

  6. STL 容器的概念

    STL 容器的概念 在实际的开发过程中,数据结构本身的重要性不会逊于操作于数据结构的算法的重要性,当程序中存在着对时间要求很高的部分时,数据结构的选择就显得更加重要. 经典的数据结构数量有限,但是我们 ...

  7. STL容器共性机制和使用场景

    一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...

  8. 从零开始写STL—容器—vector

    从0开始写STL-容器-vector vector又称为动态数组,那么动态体现在哪里?vector和一般的数组又有什么区别?vector中各个函数的实现原理是怎样的,我们怎样使用会更高效? 以上内容我 ...

  9. STL——容器(List)List 的概念

    1. List 容器的基本概念 1. list 是一个双向链表容器,可高效的进行插入删除元素,他的原理在于每个元素都有两个指针来记录前后两个元素的地址,像火车车厢一样,list 中各个元素在物理存储单 ...

随机推荐

  1. 彻底弄懂ES6中的Map和Set

    Map Map对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值.构造函数Map可以接受一个数组作为参数. Map和Object的区别 一个Object 的键只能是字符串或者 Symb ...

  2. 因改漏洞而引申了解的Cookie机制!

    近期因为修改漏洞:Appscan扫描漏洞:加密会话(SSL)Cookie中缺少Secure属性,而涉及到Cookie有关的知识,现结合该漏洞的修复过程和了解的cookie知识总结一下. 一.加密会话( ...

  3. docker实战 (3) 常规配置

    本节会持续更新,在项目实战中遇到的docker配置都会更新进来 docker常用命令: docker 介绍: what: 是什么 why: 为什么用 how: 怎么用 docker 特点: 轻量级,可 ...

  4. webdriver切换frame的方法

    iframe: iframe 就是一个特殊的html 元素, 它在原来的html 范围内,开辟了一个新的HTML. iframe 元素会创建包含另外一个文档的内联框架(即行内框架) 理解:网页嵌套网页 ...

  5. maven学习笔记四(聚合和继承)

    聚合 现在假如,我创建了3个maven项目, user-core.2.user-log,3.user-service 这个时候,假如我们要打包这些项目,要一个一个来,会很麻烦.那么我们有没有更好的办法 ...

  6. 自定义View(二),强大的Canvas

    本文转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android中使用图形处理引擎,2D部分是 ...

  7. 微信小程序转义解析渲染html

    今天开发小程序时,想调用商品详情字段,发现大部分是用编辑器编辑的html原生标签,无法在小程序直接使用. 后面自己使用正则和字符串替换,效果也不佳. 最后在网上找到了wx-mina-html-view ...

  8. MySQL/MariaDB数据库的存储引擎

    MySQL/MariaDB数据库的存储引擎 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.MySQL体系结构 连接管理模块: 负责接收远程用户的连接. 线程管理模块: 维护 ...

  9. C++学习(1)—— 初识C++

    1. 变量 作用:给一段指定的内存空间起名,方便操作这段内存空间 语法:数据类型 变量名称=变量初始值​ #include<iostream> using namespace std; i ...

  10. SQL进阶系列之4HAVING字句的力量

    写在前面 SQL是面向集合的语言,与面向过程和面向对象语言都不一样 寻找缺失的编号 /* 寻找缺失的编号 */ CREATE TABLE SeqTbl (seq INTEGER PRIMARY KEY ...