2018 코딩테스트 문제 3
#include <iostream>
#include <vector>
#include <string>
#include <list>
using namespace std;
list<string> cache;
int get_data(string city, int cacheSize)
{
int time;
auto it = find(cache.begin(), cache.end(), city);
if (it != cache.end()) { // exist
time = 1;
cache.erase(it);
}
else {
time = 5;
}
cache.push_back(city);
if (cache.size() > cacheSize) {
cache.erase(cache.begin());
}
return time;
}
int main()
{
int cacheSize=2;
vector<string> cities = {"Jeju", "Pangyo", "Seoul", "NewYork", "LA",
"SanFrancisco", "Seoul", "Rome", "Paris", "Jeju", "NewYork", "Rome"};
int sum = 0;
for (int i = 0; i < cities.size(); i++) {
sum += get_data(cities[i], cacheSize);
}
cout << sum << endl;
return 0;
}