Created
September 15, 2018 08:03
-
-
Save completejavascript/cbb115375250902b934cac750c5cc7a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
const int MAX = 105; | |
int N, M; | |
int front, rear, leng, queue[MAX]; | |
int numMinute; | |
int Abs(int a) | |
{ | |
if(a >= 0) return a; | |
return -a; | |
} | |
void Enqueue(int p) | |
{ | |
queue[rear] = p; | |
rear = (rear + 1) % MAX; | |
leng++; | |
} | |
int Dequeue() | |
{ | |
int p = queue[front]; | |
front = (front + 1) % MAX; | |
leng--; | |
return p; | |
} | |
int main() | |
{ | |
freopen("input.txt","r",stdin); | |
int T; | |
cin >> T; | |
for(int tc = 0; tc < T; tc++) | |
{ | |
// Nhập đầu vào | |
cin >> N >> M; | |
for(int i = 0; i < N; i++) | |
cin >> queue[i]; | |
// Đánh dấu vị trí công việc cần hoàn thành | |
// bằng cách chuyển về giá trị âm | |
queue[M] = -queue[M]; | |
front = 0; | |
rear = N; | |
leng = N; | |
numMinute = 0; | |
while(leng > 0) | |
{ | |
int k = Dequeue(); | |
bool IsPrint = true; | |
// Duyệt hàng đợi từ front đến rear | |
for(int i = front; i != rear; i = (i+1) % MAX) | |
{ | |
// Nếu tồn tại công việc có độ ưu tiên lớn hơn k | |
// thì cho k xuống cuối hàng đợi | |
if(Abs(k) < Abs(queue[i])) | |
{ | |
IsPrint = false; | |
Enqueue(k); | |
break; | |
} | |
} | |
// Ngược lại, nếu không tìm thấy, thì chứng tỏ k có độ ưu tiên cao nhất. | |
if(IsPrint) | |
{ | |
numMinute++; | |
// Nếu k < 0 thì đó là công việc cần tìm | |
if(k < 0) | |
{ | |
cout << numMinute << endl; | |
break; | |
} | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment