Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <algorithm>
- #include <list>
- using namespace std;
- struct car {
- int type;
- int queueNumber;
- bool operator==(const car& first) const{
- return first.type==type;
- };
- };
- bool isBigger (car car1, car car2){
- return car1.queueNumber > car2.queueNumber;
- }
- bool sortCars(car car1, car car2){
- if (car1.type < car2.type){
- return isBigger(car1, car2);
- } else return isBigger(car2, car1);
- }
- int main() {
- //Ввод данных
- int n, k, p;
- int answer = 0;
- cin >> n >> k >> p;
- vector<car> floor;
- vector<int> subsequenceOfCars;
- vector<car> cars;
- map <int, bool> carsOnFloor;
- map <int, int> isCarsInUse;
- for (int i = 1; i <= k; i++){
- carsOnFloor.insert(make_pair(i, false));
- isCarsInUse.insert(make_pair(i,0));
- }
- for (int i = 1; i <= p; i++){
- int temp;
- cin >> temp;
- subsequenceOfCars.push_back(temp);
- if (floor.empty() || (!carsOnFloor[temp] && floor.size() < k)){
- floor.push_back(car{temp, i});
- carsOnFloor[temp] = true;
- answer+=1;
- }
- else {
- isCarsInUse[temp] +=1;
- cars.push_back(car{temp, i});
- }
- }
- sort(floor.begin(), floor.end(), isBigger);
- sort(cars.begin(), cars.end(), sortCars);
- //Тело программы
- for (int i = 0; i < subsequenceOfCars.size(); i++){
- if (carsOnFloor[subsequenceOfCars[i]]){
- auto temp = find(floor.begin(), floor.end(), cars[i]) - floor.begin();
- for (int j = 0; j < cars.size(); j++) {
- if (cars[j].type == subsequenceOfCars[i]){
- if ((cars.begin() + j+1) != cars.end() && (cars.begin() + (j+1))->type == (cars.begin() + (j))->type){
- (floor.begin()+temp)->queueNumber = (cars.begin() + (j))->queueNumber;
- }else{
- (floor.begin()+temp)->queueNumber = (cars.begin() + (j))->queueNumber;
- }
- cars.erase(cars.begin() + j);
- isCarsInUse[subsequenceOfCars[i]] -=1;
- break;
- }
- }
- sort(floor.begin(), floor.end(), isBigger);
- } else {
- int index = -1;
- for (int j = 0; j < floor.size(); j++){
- if (isCarsInUse[floor[j].type] == 0){
- index = j;
- break;
- }
- }
- if (index != -1){
- carsOnFloor[subsequenceOfCars[i]] = true;
- carsOnFloor[floor[index].type] = false;
- floor.erase(floor.begin()+index);
- for (int j = 0; j < cars.size(); j++) {
- if (cars[j].type == subsequenceOfCars[i]) {
- floor.insert(floor.begin(), cars[j]);
- isCarsInUse[subsequenceOfCars[i]] -= 1;
- cars.erase(cars.begin() + j);
- break;
- }
- }
- sort(floor.begin(), floor.end(), isBigger);
- answer += 1;
- }else {
- carsOnFloor[subsequenceOfCars[i]] = true;
- carsOnFloor[floor.front().type] = false;
- floor.erase(floor.begin());
- for (int j = 0; j < cars.size(); j++) {
- if (cars[j].type == subsequenceOfCars[i]) {
- floor.insert(floor.begin(), cars[j]);
- isCarsInUse[subsequenceOfCars[i]] -= 1;
- cars.erase(cars.begin() + j);
- break;
- }
- }
- sort(floor.begin(), floor.end(), isBigger);
- answer += 1;
- }
- }
- }
- //Вывод
- cout << answer;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement