sockets – codeforce 732e

Problem

Here.

Code in C++

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
#include <map>
#include <vector>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 200000 + 5;
struct Node {
int v, id, num;
Node(int v, int id, int num): v(v), id(id), num(num) {}
bool operator <(const Node &t) const {
return (v < t.v) || (v == t.v && num > t.num);
}
};
int n, m;
int ans[maxn], ans2[maxn];
int total_socket, total_computer;
int () {
priority_queue<Node> que;
map<int, vector<int> > mp;
scanf("%d %d", &n, &m);
int x;
for (int i = 1; i <=n; i++) {
scanf("%d", &x);
mp[x].push_back(i);
}
for (int i = 1; i <= m; i++) {
scanf("%d", &x);
que.push(Node(x, i, 0));
}
total_socket = total_computer = 0;
//int max_now;
while(!que.empty()){
Node tmp = que.top(); que.pop();
if (mp.find(tmp.v) != mp.end() && mp[tmp.v].size()) {
ans[tmp.id] = tmp.num;
ans2[mp[tmp.v][mp[tmp.v].size()-1]] = tmp.id;
total_socket += tmp.num;
total_computer ++;
mp[tmp.v].pop_back();
}
else {
if (tmp.v > 1) {
que.push(Node(tmp.v - tmp.v/2, tmp.id, tmp.num+1));
}
}
}
printf("%d %dn", total_computer, total_socket);
for (int i = 1; i < m; i++)
printf("%d ", ans[i]);
printf("%dn", ans[m]);
for (int i = 1; i < n; i++)
printf("%d ", ans2[i]);
printf("%dn", ans2[n]);
return 0;
}