Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <algorithm>
- using namespace std;
- int main() {
- //Ввод данных
- int n, k, p;
- int answer = 0;
- vector<int> floor;
- cin >> n >> k >> p;
- map<int, vector<int>> carsMap;
- vector<int> subsequenceOfCars;
- for (int i = 1; i <= p; i++){
- int temp;
- cin >> temp;
- subsequenceOfCars.push_back(temp);
- if (floor.empty() || (find(begin(floor), end(floor), temp) == end(floor) && floor.size() < k)){
- floor.push_back(temp);
- answer+=1;
- }
- if (carsMap.count(temp) == 0) {
- vector<int> tempVec = {i};
- carsMap.insert(make_pair(temp, tempVec));
- } else {
- carsMap[temp].push_back(i);
- }
- }
- //Тело программы
- for (auto i : subsequenceOfCars){
- auto temp = find(begin(floor), end(floor), i);
- if (temp != end(floor)){
- carsMap[i].erase(carsMap[i].begin());
- } else {
- int max = 0;
- int tempInd = 0;
- for (auto j : carsMap){
- if (j.second.empty() && find(begin(floor), end(floor), j.first) != end(floor)){
- tempInd = j.first;
- break;
- }
- if (!j.second.empty() && find(begin(floor), end(floor), j.first) != end(floor) && (j.first != i)&& *(j.second.begin()) > max){
- max = *(j.second.begin());
- tempInd = j.first;
- }
- }
- if (tempInd != 0) {
- temp = find(begin(floor), end(floor), tempInd);
- floor.erase(temp);
- floor.insert(floor.begin(), i);
- carsMap[i].erase(carsMap[i].begin());
- answer += 1;
- } else {
- floor.erase(floor.begin());
- floor.insert(floor.begin(), i);
- carsMap[i].erase(carsMap[i].begin());
- answer += 1;
- }
- }
- }
- //Вывод
- cout << answer;
- return 0;
- }
Add Comment
Please, Sign In to add comment