CCF 学生排队问题

时间:2023-01-20 21:35:00

CCF 学生排队问题
核心思想:这个问题是随机的插入和删除,因此选择链表list作为容器,过程中通过insert和eraser来进行模拟插入和删除的操作。

#include<iostream>
#include<list>
#include<algorithm>
#include<iterator>
using namespace std;

void print(list<int> &student)
{
    list<int>::iterator bb = student.begin();
    for (; bb != student.end(); bb++)
    {
        cout << *bb << " ";
    }
    cout << endl;
}

int main()
{
    int s, c;
    cin >> s >> c;
    list<int> student;
    for (int i = 0; i < s; i++)
        student.push_back(i + 1);
    for (int i = 0; i < c; i++)
    {
        int st, change;
        cin >> st >> change;
        list<int>::iterator start = find(student.begin(), student.end(), st),run(start);    
        if (change > 0)
        {
            change += 1;
            int i = 0;
            while (i<change)
            {
                if (start == student.end())
                {
                    break;
                }
                start++;
                i++;                                                    
            }
            student.insert(start, st);
        }
        else
        {
            int i = 0;
            while (i > change)
            {
                if (start == student.begin())
                {
                    break;
                }
                start--;
                i--;                
            }
            student.insert(start, st);
        }
        student.erase(run);     
    }   
    print(student);
    return 0;
}