[Code Jam 2022 Qualification Round 2022] Twisty Little Passages

Twisty Little Passages

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
103
104
105
106
107
108
109
110
111
#include <bits/stdc++.h>
#include <cstdlib>
using namespace std;
pair<int, int> input() {
int room, pass;
cin>>room>>pass;
return {room, pass};
}
void operationWalk() {
cout<<"W"<<endl;
}
void operationTeleport(int n) {
cout<<"T "<<n<<endl;
}
vector<int> initVisOrder(int n) {
vector<int> order;
for(int i = 1; i <= n; i++)
order.push_back(i);

for(int i = n - 1; i > 0; i--) {
int r = rand() % i;
swap(order[r], order[i]);
}
return order;
}
string exactJustify(int n) {
long long res = 0;
auto [r, p] = input();
res += p;
for(int i = 0; i < n; i++) {
if(i + 1 == r) continue;
operationTeleport(i + 1);
auto [room, pass] = input();
res += pass;
}
return "E " + to_string(res / 2);
}
int pickRandom(vector<int>& order, unordered_set<int>& vis) {
while(vis.count(order.back()))
order.pop_back();
return order.back();
}
string guessJustify(int n, int k) {
unordered_set<int> vis;
vector<int> visOrder = initVisOrder(n);

vector<int> avg(n + 1), odd(n + 1);
auto [r, p] = input();
avg[r] = p;

vis.insert(r);

for(int i = 0; i < k / 2; i++) {
operationWalk();
auto [wr, wp] = input();
odd[wr] = wp;
avg[wr] = 0;
vis.insert(wr);

operationTeleport(pickRandom(visOrder, vis));
auto [tr, tp] = input();
avg[tr] = tp;
vis.insert(tr);
}

if(k & 1) {
operationWalk();
auto [wr, wp] = input();
odd[wr] = wp;
avg[wr] = 0;
vis.insert(wr);
}

long long res = 0;

long long sum = 0, avgCnt = 0, oddSum = 0, oddCnt = 0;

for(auto& pass : avg) {
sum += pass;
avgCnt += (pass > 0);
}

for(auto& pass : odd) {
oddSum += pass;
oddCnt += (pass > 0);
}

res = oddSum + (1.0 * sum / avgCnt) * (n - oddCnt);

return "E " + to_string(res / 2);
}
string solve(int n, int k) {
if(n - 1 <= k) {
return exactJustify(n);
} else {
return guessJustify(n, k);
}
}

int main() {
srand((unsigned int)time(NULL));
int tc;
cin>>tc;
for(int i = 1; i <= tc; i++) {
int n,k;
cin>>n>>k;
cout<<solve(n,k)<<endl;
}
return 0;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/03/PS/Google/twisty-little-passages/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.