-
Notifications
You must be signed in to change notification settings - Fork 0
/
1056.cpp
102 lines (82 loc) · 2.09 KB
/
1056.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <cstdio>
#include <queue>
#include <vector>
#include <functional>
#include <algorithm>
const int MAXN = 1000 + 10;
struct Node {
int id, data, rank;
struct cmp {
bool operator()(Node *a, Node *b) {
return a->data > b->data;
}
};
} a[MAXN];
int ranks[MAXN];
void getWait(std::priority_queue<Node*, std::vector<Node*>, Node::cmp> &pq, std::queue<Node*> &wait, int turn) {
while (pq.size() > 1) {
Node* tmp = pq.top();
pq.pop();
tmp->rank = turn;
}
if (!pq.empty()) {
wait.push(pq.top());
pq.pop();
}
}
int main() {
int n, maxSize;
scanf("%d%d", &n, &maxSize);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].data);
a[i].id = i;
}
std::vector<Node*> t;
std::queue<Node*> wait;
std::priority_queue<Node*, std::vector<Node*>, Node::cmp> pq;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
t.push_back(&a[x]);
}
int turn = 0;
while (true) {
if (t.size() == 1) {
t[0]->rank = turn;
break;
}
int cnt = 0;
for (auto x : t) {
pq.push(x);
cnt++;
if (cnt >= maxSize) {
getWait(pq, wait, turn);
cnt = 0;
}
}
getWait(pq, wait, turn);
turn++;
t.clear();
while (!wait.empty()) {
t.push_back(wait.front());
wait.pop();
}
}
std::sort(a, a + n, [](Node a, Node b)->bool{return a.rank > b.rank;});
int rank = 1, numOfSame = 0, pre = -1;
for (int i = 0; i < n; i++) {
if (a[i].rank != pre) {
rank += numOfSame;
numOfSame = 1;
} else {
numOfSame++;
}
ranks[a[i].id] = rank;
pre = a[i].rank;
}
for (int i = 0; i < n; i++) {
if (i != n - 1) printf("%d ", ranks[i]);
else printf("%d\n", ranks[i]);
}
return 0;
}