題目很簡單,給一個隊列以及文件的位置,然後一個一個檢查,如果第一個是優先級最高的就打印,否則放到隊列後面,求所要打印的文件打印需要花費多長時間。
這裡我用數組模擬隊列實現,考慮到最糟糕的情況,必須把數組開到maxn*maxn。另外當所要打印的文件優先級不是最高的時候也需要排列到後面。
0.016s。
代碼:
#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 }