๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 13414๋ฒˆ - ์ˆ˜๊ฐ•์‹ ์ฒญ

by dkswnkk 2021. 11. 4.

๋ฌธ์ œ

 

13414๋ฒˆ: ์ˆ˜๊ฐ•์‹ ์ฒญ

์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ž…๋ ฅ์€ 1๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ณผ๋ชฉ์˜ ์ˆ˜๊ฐ• ๊ฐ€๋Šฅ ์ธ์› K(1 ≤ K ≤ 100,000)์™€ ํ•™์ƒ๋“ค์ด ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ์ˆœ์„œ๋ฅผ ๊ธฐ๋กํ•œ ๋Œ€๊ธฐ๋ชฉ

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <map>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	//์‹œ๊ฐ„ ์ดˆ๊ณผ ์ฝ”๋“œ
	/*
	int k, L; cin >> k >> L;
	vector<int>ans;
	for (int i = 0; i < L; i++) {
		int sno; cin >> sno;
		if (find(ans.begin(), ans.end(), sno) != ans.end()) {	//์ค‘๋ณต๋˜๋Š” ๊ฐ’์ด ์žˆ์„๊ฒฝ์šฐ
			auto it = find(ans.begin(), ans.end(), sno) - ans.begin();
			ans.erase(ans.begin() + it);	//์‚ญ์ œํ•˜๊ณ 
			ans.push_back(sno);	//๋งจ๋’ค์— ์‚ฝ์ž…
		}
		else ans.push_back(sno);	//์ค‘๋ณต๋˜๋Š” ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ ์‚ฝ์ž…
	}
	for (int i = 0; i < k; i++) {
		cout << ans[i] << "\n";
	}
	*/

	map<string, int>m;
	vector<pair<int, string>>v;

	int k, L; cin >> k >> L;
	for (int i = 0; i < L; i++) {
		string number; cin >> number;
		m[number] = i + 1;
	}

	for (auto i = m.begin(); i != m.end(); i++) {
		string a = i->first;
		int b = i->second;
		v.push_back({ b,a });
	}

	sort(v.begin(), v.end());
	for (int i = 0; i < min(k,(int)v.size()); i++) {	//์ถœ๋ ฅ
		cout << v[i].second << "\n";
	}
}

๋Œ“๊ธ€