기술 블로그

1966번 프린터 큐 본문

알고리즘 문제/BOJ

1966번 프린터 큐

parkit 2018. 12. 5. 22:11
728x90
반응형

https://www.acmicpc.net/problem/1966




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
 
using namespace std;
 
bool cmp(int &a, int &b)
{
    return a > b;
}
 
int main(void)
{
    int T = 0, N = 0, M = 0, document = 0;
 
    scanf("%d"&T);
 
    while (T--)
    {
        queue<pair<intint> > q;
        vector<int> v;
        bool stop = false;
        int p = 0;
        
        scanf("%d %d"&N, &M);
 
        for (int i = 0; i < N; i++)
        {
            scanf("%d"&document);
 
            q.push({ i, document });
 
            v.push_back(document);
        }
 
        sort(v.begin(), v.end(), cmp);
 
        for (int i = 0; i < v.size(); i++)
        {
            while (1)
            {
                if (v.at(i) == q.front().second)
                {
                    ++p;
 
                    if (M == q.front().first)
                    {
                        stop = true;
                    }
 
                    if (!q.empty()) q.pop();
 
                    break;
                }
                else
                {
                    q.push(q.front());
                    q.pop();
                }
            }
 
            if (stop)
            {
                break;
            }
        }
 
        printf("%d\n", p);
    }
    
    return 0;
}
cs

















728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

1021번 회전하는 큐  (0) 2018.12.09
1057번 토너먼트  (0) 2018.12.07
2668번 숫자고르기  (0) 2018.12.04
4641번 Doubles  (0) 2018.12.03
10451번 순열 사이클  (0) 2018.12.03