The question is very simple: Give a queue and file location, and then check one by one. If the first is the highest priority, print it; otherwise, put it behind the queue, how long does it take to print the file to be printed.
Here I use arrays to simulate the queue implementation. In the worst case, we must open the array to maxn * maxn. In addition, when the priority of the file to be printed is not the highest, it needs to be sorted to the back.
0.016 s.
Code:
#include <cstdio> const int maxn = 101; int t, n, m, time; int q[maxn*maxn]; int print() { int front = 0, rear = n; while (1) { int max = q[front]; for (int i = front; i < rear; i++) if (q[i] > max) { if (front == m) m = rear; q[rear++] = q[front++]; break; } else if (i == rear - 1) { time++; // printf("%d %d\n", time, q[front]); if (front == m) return time; front++; } }//while } int main() { scanf("%d", &t); while (t--) { time = 0; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &q[i]); printf("%d\n", print()); }//while } #include <cstdio>const int maxn = 101;int t, n, m, time;int q[maxn*maxn];int print() {int front = 0, rear = n;while (1) {int max = q[front];for (int i = front; i < rear; i++)if (q[i] > max){if (front == m)m = rear;q[rear++] = q[front++];break;}else if (i == rear - 1){time++;//printf("%d %d\n", time, q[front]);if (front == m)return time;front++;}}//while}int main() {scanf("%d", &t);while (t--) {time = 0;scanf("%d%d", &n, &m);for (int i = 0; i < n; i++)scanf("%d", &q[i]);printf("%d\n", print());}//while}