[R11B]前三小
// 5555555555555.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
题目链接: https://bs.daimayuan.top/p/62
题目名称: #62. [R11B]前三小
题目类型: 模拟
时空限制: 1秒/512MB
难度: 3
题目描述
给定一个长度为 n 的整数数组 A,其中的数字互不相同。
求出其中前三小的数字,按在数组中出现的顺序输出。
输入格式
第一行包含一个整数 n 表示数组长度。
第二行包含 n 个不同整数,表示数组 A。
输出格式
输出的第一行包含三个整数,表示 A 中前三小的数字。按在数组中出现的顺序输出。
输入:
5
7 3 4 9 1
输出:
3 4 1
样例解释 #1:
A 中前三小的数字分别是:1,3,4。在数组中出现的顺序为:3,4,1。
数据规模
对于 100% 的数据,3≤n≤105,1≤Ai≤109。数据保证 A 中的数字互不相同。
*/
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int n;
vector<int> ans;
int main()
{
cin >> n;
for (int i = 0; i < n; i++) {
int t; cin >> t;
if (ans.size() == 3) {
int findidx = -1;
for (int j = 0; j < 3; j++) {
if (ans[j] > t) {
if (findidx == -1 || (ans[j] > ans[findidx])) {
findidx = j;
}
}
}
if (findidx != -1) {
ans.erase(ans.begin() + findidx);
ans.push_back(t);
}
}
else {
ans.push_back(t);
}
}
for(int i= 0;i< ans.size();i++) {
cout << ans[i] << " ";
}
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

