标签:algorithm proc other 选择 als 出队 you 后端 mes
<font size=5 face"微软雅黑">STL初步
<font size=3 face"微软雅黑">提交ACM会TLE /仅以学习STL与迭代器使用
<font size=5 face"微软雅黑">C. Cards Sorting
<font size=4 face"微软雅黑">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.
<font size=5 face"微软雅黑">Input
<font size=4 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.
<font size=5 face"微软雅黑">Output
<font size=4 face"微软雅黑">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队列定义在头文件
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。
还原题意既是取对头一张牌符合条件就pop出队头第一张,不符合就将第一张pop出后插入至队尾。
接着只需要判断front()与当前最小值是否相等即可,最小值可以用
begin() ,返回set容器第一个元素的迭代器
end() ,返回一个指向当前set末尾元素的下一位置的迭代器.
clear() ,删除set容器中的所有的元素
empty() ,判断set容器是否为空
max_size() ,返回set容器可能包含的元素最大个数
size() ,返回当前set容器中的元素个数
rbegin() ,返回的值和end()相同
rend() ,返回的值和begin()相同
使用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();
}
}
}
标签:algorithm proc other 选择 als 出队 you 后端 mes
原文地址:https://www.cnblogs.com/xiaofengqaq/p/11250860.html