[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;
}

 

posted on 2025-07-09 14:41  itdef  阅读(6)  评论(0)    收藏  举报

导航